Spring Series "5" @Autowired detailed (supplementary)

Source: Internet
Author: User

Note: The following is based on version Spring2.5.

A total of four kinds of @autowired usage, the first method of construction used in the previous section has been explained, this time to explain the other three kinds.

Now there is a boss Boss.java, who owns a car and an office.

 Public class Boss {      private  car car;       Private Office Office;         // Omit Get/setter         @Override      public  String toString () {          return "car:" + car + "\ n" + "office:" + office;      }  }  

The spring configuration file is as follows: declaring the Autowiredannotationbeanpostprocessor class

<?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">    <Beanclass= "Org.springframework.beans.factory.annotation." Autowiredannotationbeanpostprocessor "/>    <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>

1. @Autowired the code that automatically injects member variables: the car and office classes are done by themselves. If this is not possible, it is recommended not to look down, back to the Java Foundation.

Import org.springframework.beans.factory.annotation.Autowired;  Public class Boss {    @Autowired    private  car car;    @Autowired    Private  Office office;         // omit Getter/setter Method }

2. @Autowired The use of setter methods.

This method is not explained.

3. @Autowired use on any method, as long as the method defines the parameters that need to be injected to implement the bean injection.

When the spring container is started, Autowiredannotationbeanpostprocessor will scan all the beans in the spring container and find matches (by default by type) when they find @Autowired annotations in the bean. An, and inject it into the corresponding place.

Spring Series "5" @Autowired detailed (supplementary)

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.