How to inject value into bean properties in Spring
In Spring, there are three ways to inject value into bean properties.
- Normal way
- Shortcut
- “p” schema
See a simple Java class, which contains two properties – name
and type
. Later you will use Spring to inject value into the bean properties.
package com.mkyong.common;
public class FileNameGenerator {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
1. Normal way
Inject value within a ‘value
’ tag and enclosed with ‘property
’ tag.
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
<property name="name">
<value>mkyong</value>
</property>
<property name="type">
<value>txt</value>
</property>
</bean>
</beans>
2. Shortcut
Inject value with “value
” attribute.
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
<property name="name" value="mkyong" />
<property name="type" value="txt" />
</bean>
</beans>
3. “p” schema
Inject value by using “p
” schema as an attributes.
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator" p:name="mkyong" p:type="txt" />
</beans>
Remember declares the xmlns:p=”http://www.springframework.org/schema/p
in the Spring XML bean configuration file.
Conclusion
Which methods to use is totally base on personal preference, it will not affect the value inject into the bean properties.
相關文章
- How to load multiple Spring bean configuration fileSpringBean
- how to get sharepoint lookup value
- [Spring]BeanSpringBean
- How to update BOL entity property value via ABAP code
- Python演算法-How to sort a dictionary by valuePython演算法
- Spring中Bean及@Bean的理解SpringBean
- Spring Bean容器SpringBean
- 【Spring】Bean管理SpringBean
- 玩轉spring boot——properties配置Spring Boot
- Spring Bean 綜述SpringBean
- Spring bean 裝配SpringBean
- spring boot factory beanSpring BootBean
- Spring基礎(Bean)SpringBean
- Spring Bean作用域SpringBean
- Spring Bean 詳解SpringBean
- 淺談Spring BeanSpringBean
- Spring Bean裝配SpringBean
- Spring中的BeanSpringBean
- Spring – 裝配BeanSpringBean
- spring引入properties變數報錯Spring變數
- Spring裝配Bean(六)Bean的作用域SpringBean
- Spring Bean Scope 有狀態的Bean和無狀態的BeanSpringBean
- Spring系列.Bean簡介SpringBean
- 《Spring》(六)---- Bean的scopeSpringBean
- 二、Spring裝配BeanSpringBean
- Spring Bean生命週期SpringBean
- Spring Bean 標籤解析SpringBean
- Spring裝配Bean(四)SpringBean
- Spring Bean 生命週期SpringBean
- Spring IOC與Bean容器SpringBean
- Spring裝配Bean(一)SpringBean
- Spring裝配Bean(二)SpringBean
- spring bean建立細節SpringBean
- spring註解中使用properties檔案Spring
- Spring多資原始檔properties的配置Spring
- 在SAP ABAP裡使用註解@Inject模擬Java SpringJavaSpring
- Spring IOC 一——Spring容器裝配BeanSpringBean
- Spring 學習筆記(2) Spring BeanSpring筆記Bean