Spring managed bean dependency injection (Set Value injection)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.