Spring @Autowired annotations and automatic assembly

Source: Internet
Author: User

1 Ways to configure files

It is generally time to write the spring Framework code. Always follow a rule: All beans injected in spring are recommended to be defined as private domain variables. Also write the Get and set methods.
Boss has two properties for Office and Car types

1  Public classBoss {2     Privatecar Car; 3     PrivateOffice Office; 4      5     //omitted Get/setter not written6      7 @Override8      PublicString toString () {9         return"Car:" + car + "\ n" + "office:" +Office; Ten     }       One}

We declare Office and Car as beans in the Spring container and inject it into the Boss bean: Here is the configuration file Beans.xml and test class code that uses traditional XML to do this work

<?XML version= "1.0" encoding= "UTF-8"?>      <Beansxmlns= "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 ">          <BeanID= "Boss"class= "Com.baobaotao.Boss">              < Propertyname= "Car"ref= "Car"/>              < Propertyname= "Office"ref= "Office" />          </Bean>          <BeanID= "Office"class= "Com.baobaotao.Office">              < Propertyname= "Officeno"value= "002"/>          </Bean>          <BeanID= "Car"class= "Com.baobaotao.Car"Scope= "Singleton">              < Propertyname= "Brand"value= "Red Flag CA72"/>              < Propertyname= "Price"value= "$"/>          </Bean>      </Beans>     
1 ImportOrg.springframework.context.ApplicationContext; 2 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; 3  Public classAnnoioctest {4      5      Public Static voidMain (string[] args) {6String[] Locations = {"Beans.xml"}; 7ApplicationContext CTX =8             NewClasspathxmlapplicationcontext (locations); 9Boss Boss = (boss) Ctx.getbean ("Boss")); Ten System.out.println (Boss);  One     }       A}

2, through the annotation method to achieve injection

Spring 2.5 introduces @Autowired annotations that allow you to annotate class member variables, methods, and constructors, and complete the task of automating Assembly. Use the @Autowired to eliminate the set, get method.
To achieve the purpose of streamlining our procedures. This is required to handle:

* Add in Applicationcontext.xml:

<!--the beanpostprocessor will automatically inject the Bean labeled @Autowired-
<bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

<!--the beanpostprocessor will automatically inject the Bean labeled @Autowired-
<bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Spring parses @Autowired with a beanpostprocessor, so @Autowired must be declared in the Spring container beforehand Autowiredannotationbeanpostprocessor Bean.

* Modify the method of the bean in the original injection SPIRNG container, add the label @autowired to the domain variable, and remove the corresponding get and set methods.

 Public class Boss {               @Autowired         private  car car;               @Autowired         Private  Office office;                // omit ToString not written      }     

In Applicatoncontext.xml, the original boss Reference <porpery > tags are also removed.

<?XML version= "1.0" encoding= "UTF-8"?>      <Beansxmlns= "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 ">               <!--the Beanpostprocessor will automatically function to automatically inject the Bean labeled @Autowired -          <Beanclass= "Org.springframework.beans.factory.annotation." Autowiredannotationbeanpostprocessor "/>               <!--Remove the attribute injection configuration information for the boss Bean -          <BeanID= "Boss"class= "Com.baobaotao.Boss"/>                 <BeanID= "Office"class= "Com.baobaotao.Office">              < Propertyname= "Officeno"value= "001"/>          </Bean>          <BeanID= "Car"class= "Com.baobaotao.Car"Scope= "Singleton">              < Propertyname= "Brand"value= "Red Flag CA72"/>              < Propertyname= "Price"value= "$"/>          </Bean>      </Beans>    

This way, when the spring container starts, Autowiredannotationbeanpostprocessor will scan all the beans in the spring container, and when it finds that the bean has @Autowired Note, the Bean that matches it (the default by type) is found and injected into the corresponding place.

According to the above configuration, Spring will directly use the Java reflection mechanism to automatically inject the private member variables of car and office in the Boss. So when you use @Autowired on member variables, you can delete their setter methods (Setcar () and Setoffice ()) from the Boss.

Reference: http://wangqiaowqo.iteye.com/blog/1088397


Spring @Autowired annotations and automatic assembly

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.