In spring, there are three ways to inject values into bean properties.
- The normal way
- Shortcut
- "P" mode
See a simple Java class that contains two properties-name and type. You will use spring to inject values into this bean property later.
Package Com.yiibai.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. The normal method injects a value into a "value" tag and ends with a "property" tag.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "><bean id=" Filenamegenerator "class=" Com.yiibai.common.FileNameGenerator "><property name=" name "><value>yiibai</value></ Property><property name= "Type" ><value>txt</value></property></bean></beans >
2, the shortcut injects the value of the "Value" property.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "><bean id=" Filenamegenerator "class=" Com.yiibai.common.FileNameGenerator "><property name=" name "value=" Yiibai "/><property name=" type "value = "TXT"/></bean></beans>
3. The "P" mode is injected into a property by using "P" mode.
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">< Bean id= "Filenamegenerator" class= "Com.yiibai.common.FileNameGenerator" p:name= "Yiibai" p:type= "TXT"/>< /beans>
Remember to declare xmlns:p= "http://www.springframework.org/schema/p" in the Spring XML bean configuration file. Summarizing the use of these methods is entirely based on personal preference and does not affect the value of the injected Bean property. Http://www.yiibai.com/spring/how-to-define-bean-properties-in-spring.html
How to inject values into spring bean properties