Set-value injection is one of the many dependency injection types supported by spring and the most common one. Setter injection is used to call the setter method after instantiating the managed bean by calling the non-parametric Constructor (or the non-parametric static factory method, or the factory Bean's pipeline static factory method, to establish the dependency between objects.
1. The keyless constructor instantiates the managed Bean
Public class helloworld implements ihelloworld {</P> <p> protected static final log = logfactory. getlog (helloworld. class); <br/> private ihellostr hellostr; </P> <p> Public helloworld () {<br/> super (); <br/>}< br/> Public void sethellostr (ihellostr Str) {<br/> This. hellostr = STR; <br/>}< br/> Public String getcontent () {<br/> return hellostr. getcontent (); <br/>}< br/>}
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans <br/> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <br/> <bean name = "filehello" class = "test. filehellostrimpl "> <br/> <constructor-Arg> <br/> <value> helloworld. properties </value> <br/> </constructor-Arg> <br/> </bean> <br/> <bean id = "helloworld" class = "test. helloworld "> <br/> <property name =" hellostr "ref =" filehello "/> <br/> </bean> <br/> </beans>
You can use the ref attribute to specify the dependent object or value attribute to directly give the target object.
Ii. Non-parameter static factory method instantiate managed Bean
If you want to obtain the daily managed bean by calling the no-argument static factory method, you need to add the static method to the corresponding pojo class, as shown in the following example:
Public class helloworld implements ihelloworld {</P> <p> protected static final log = logfactory. getlog (helloworld. class); <br/> private ihellostr hellostr; <br/> Public void sethellostr (ihellostr Str) {<br/> This. hellostr = STR; <br/>}< br/> Public String getcontent () {<br/> return hellostr. getcontent (); <br/>}</P> <p> Public static helloworld gethelloworld () {<br/> return New helloworld (); <br/>}< br/>}
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans <br/> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <br/> <bean id = "helloworld" factory-method = "gethelloworld" <br/> class = "test. helloworld "> <br/> <property name =" hellostr "ref =" filehello "/> <br/> </bean> </P> <p> <bean name = "filehello" class = "test. filehellostrimpl "> <br/> <constructor-Arg> <br/> <value> helloworld. properties </value> <br/> </constructor-Arg> <br/> </bean> </P> <p> </beans>
3. Managed bean of factory bean non-static factory Method Instance
Similarly, if you want to obtain the managed bean by calling the factory bean non-static factory method, you need to add the non-static method to the corresponding pojo class. The example is as follows:
Public class helloworld implements ihelloworld {</P> <p> protected static final log = logfactory. getlog (helloworld. class); <br/> private ihellostr hellostr; <br/> Public void sethellostr (ihellostr Str) {<br/> This. hellostr = STR; <br/>}< br/> Public String getcontent () {<br/> return hellostr. getcontent (); <br/>}</P> <p> Public helloworld makehelloworld () {<br/> return New helloworld (); <br/>}< br/>}
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans <br/> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <br/> <bean id = "helloworldfactory" class = "test. helloworld "/> </P> <p> <bean id =" helloworld "factory-bean =" helloworldfactory "<br/> factory-method =" makehelloworld "> <br/> <property name = "hellostr" ref = "filehello"/> <br/> </bean> </P> <p> <bean name = "filehello" <br/> class = "test. filehellostrimpl "> <br/> <constructor-Arg> <br/> <value> helloworld. properties </value> <br/> </constructor-Arg> <br/> </bean> </P> <p> </beans>
The configured helloworldfactory managed bean plays the role of the factory bean.