Spring @ autowired annotation and automatic assembly

Source: Internet
Author: User

1. configuration file Method

 

When we compile the Spring framework code. The rule is always followed: All beans injected in spring are built to define private domain variables. The get and set methods must be provided.

 

Boss has two attributes of the Office and car types:


Listing 3. Boss. Java

 

Package COM. baobaotao; </P> <p> public class boss {<br/> private car; <br/> private office Office; </P> <p> // omit get/setter </P> <p> @ override <br/> Public String tostring () {<br/> return "car: "+ car +"/N "+" Office: "+ Office; <br/>}< br/>}

System. Out. println must implement the tostring method.

In the spring container, we declare office and car as beans and inject them into the boss bean. below is the configuration file beans. XML that uses traditional XML to complete this job:


Listing 4. Beans. xml configure the preceding three classes as beans

<? 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 = "boss" class = "com. baobaotao. boss "> <br/> <property name =" car "ref =" car "/> <br/> <property name =" office "ref =" office "/> <br/> </bean> <br/> <bean id = "office" class = "com. baobaotao. office "> <br/> <property name =" officeno "value =" 002 "/> <br/> </bean> <br/> <bean id =" car" class = "com. baobaotao. car "Scope =" Singleton "> <br/> <property name =" brand "value =" Hongqi ca72 "/> <br/> <property name =" price "value = "2000"/> <br/> </bean> <br/> </beans>



When we run the following code, the console correctly displays the boss information:


Listing 5. Test class: annoioctest. Java

Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class annoioctest {</P> <p> Public static void main (string [] ARGs) {<br/> string [] Locations = {"beans. XML "}; <br/> applicationcontext CTX = <br/> New classpathxmlapplicationcontext (locations); <br/> boss = (boss) CTX. getbean ("boss"); <br/> system. out. println (boss); <br/>}< br/>


This indicates that the spring container has correctly completed bean creation and assembly.

 

 

2 @ autowired

 

Spring 2.5 introduces @ autowired annotations, which can mark class member variables, methods, and constructors to complete automatic assembly. Use @ autowired to remove the set and get methods.

 

 

To achieve our goal of streamlining the program. You need to handle it like this:

* Add the following content to applicationcontext. xml: 
<! -- The beanpostprocessor will automatically inject the bean labeled @ autowired --> <br/> <Bean class = "org. springframework. Beans. Factory. annotation. autowiredannotationbeanpostprocessor"/> 

Spring parses @ autowired through a beanpostprocessor, so to make @ autowired work, you must declare autowiredannotationbeanpostprocessor bean in the spring container in advance.

* Modify the bean method in the original spirng container. 
Add @ autowired to the domain variable and remove the corresponding get and set methods.

 

Listing 6. Use boss. Java annotated with @ autowired

Package COM. baobaotao; <br/> Import Org. springframework. beans. factory. annotation. autowired; </P> <p> public class boss {</P> <p> @ autowired <br/> private car; </P> <p> @ autowired <br/> private office Office; </P> <p>... <Br/>} 

 

 

* Remove the originally referenced <porpery> label in applicatoncontext. xml.

 

<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"> </P> <p> <! -- This beanpostprocessor automatically takes effect and automatically injects the bean labeled @ autowired --> <br/> <Bean class = "org. springframework. beans. factory. annotation. <br/> autowiredannotationbeanpostprocessor "/> </P> <p> <! -- Remove the property injection configuration information of the boss Bean --> <br/> <bean id = "boss" class = "com. baobaotao. boss "/> </P> <p> <bean id =" office "class =" com. baobaotao. office "> <br/> <property name =" officeno "value =" 001 "/> <br/> </bean> <br/> <bean id =" car" class = "com. baobaotao. car "Scope =" Singleton "> <br/> <property name =" brand "value =" Hongqi ca72 "/> <br/> <property name =" price "value = "2000"/> <br/> </bean> <br/> </beans> 

In this way, when the spring container is started, autowiredannotationbeanpostprocessor will scan all the beans in the spring container, and find the bean that matches with the @ autowired annotation in the bean (by default, by type, and inject it to the corresponding place.

According to the above configuration, spring will directly use the Java reflection mechanism to automatically inject the private member variables car and office in the boss. After @ autowired is used for member variables, you can delete their setter methods (setcar () and setoffice () from the boss.

Of course, you can also use @ autowired to mark the method or constructor. If the constructor has two input parameters: bean1 and bean2, @ autowired searches for beans that match their types and uses them as input parameters of countryservice (bean1 bean1, bean2 bean2) to create countryservice beans. Let's look at the following code:

 

Package COM. baobaotao; </P> <p> public class boss {<br/> private car; <br/> private office Office; </P> <p> @ autowired <br/> Public void setcar (car) {<br/> This. car = car; <br/>}</P> <p> @ autowired <br/> Public void setoffice (office Office) {<br/> This. office = Office; <br/>}< br/>... <Br/>} 

 

@ Autowired searches for beans of the input parameter type of the labeled method and calls the method to automatically inject these beans. The following method is used to mark the constructor:

Package COM. baobaotao; </P> <p> public class boss {<br/> private car; <br/> private office Office; </P> <p> @ autowired <br/> Public boss (car, office Office) {<br/> This. car = car; <br/> This. office = Office; <br/>}</P> <p>... <Br/>} 

 

Since the boss () constructor has two input parameters: Car and office, @ autowired will respectively search for beans that match their types and use them as boss (car, office Office) to create the boss 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.