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)