@autowired annotations and automatic assembly in spring

Source: Internet
Author: User

1 Using the configuration file method to complete the automatic assembly
When we write the code for the Spring framework. 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.
For example, bosses have two properties for Office and Car types:
public class Boss {
Private car car;
Private Office Office;

Omit Get/setter

@Override
Public String toString () {
Return "car:" + car + "/n" + "office:" + office;
}
}
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 that uses traditional XML to do this work:
Beans.xml configures the above three classes as beans, and the spring container is automatically assembled.
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "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 ">
<bean id= "Boss" class= "Com.baobaotao.Boss" >
<property name= "Car" ref= "car"/>
<property name= "office" ref= "office"/>
</bean>
<bean id= "Office" class= "Com.baobaotao.Office" >
<property name= "Officeno" value= "002"/>
</bean>
<bean id= "Car" class= "Com.baobaotao.Car" scope= "singleton" >
<property name= "brand" value= "Red flag CA72"/>
<property name= "Price" value= "$"/>
</bean>
</beans>
When we run the following code, the console will correctly punch the boss's message:
public class Annoioctest {

public static void Main (string[] args) {
String[] Locations = {"Beans.xml"};
ApplicationContext CTX =
New Classpathxmlapplicationcontext (locations);
Boss Boss = (boss) Ctx.getbean ("Boss");
SYSTEM.OUT.PRINTLN (boss);
}
}
This means that the Spring container has correctly completed the work of Bean creation and assembly.

2 using @autowired annotations to complete automatic assembly
Spring 2.5 introduces @Autowired annotations that allow you to annotate class member variables, methods, and constructors, and complete the task of automating Assembly. The use of @Autowired to eliminate the set, get method, simplify the program code.
To achieve the purpose of streamlining our procedures. This is required to handle:
① added in Applicationcontext.xml:
<!--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.

② modifies the method of the bean in the original injected SPIRNG container.
Add 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;

...
}

③ the original quoted <porpery > tags in the applicatoncontext.xml.
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "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 function automatically and automatically inject the Bean labeled @Autowired
<bean class= "Org.springframework.beans.factory.annotation.
Autowiredannotationbeanpostprocessor "/>

<!--Remove the attribute injection configuration information from the boss Bean--
<bean id= "Boss" class= "Com.baobaotao.Boss"/>

<bean id= "Office" class= "Com.baobaotao.Office" >
<property name= "Officeno" value= "001"/>
</bean>
<bean id= "Car" class= "Com.baobaotao.Car" scope= "singleton" >
<property name= "brand" value= "Red flag CA72"/>
<property name= "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 for member variables, you can delete their setter methods (Setcar () and Setoffice ()) from the Boss.

Of course, you can also annotate a method or constructor by @Autowired.
public class Boss {
Private car car;
Private Office Office;

@Autowired
public void Setcar (car car) {
This.car = car;
}

@Autowired
public void Setoffice (office office) {
This.office = Office;
}
...
}
Then@Autowired The Bean that will find the parameter type of the method being labeledand invoke methods to inject these beans automatically. The constructor is annotated with the following usage method:
public class Boss {
Private car car;
Private Office Office;

@Autowired
Public Boss (car car, office office) {
This.car = car;
This.office = Office;
}

...
}

Because the boss () constructor has two entry parameters, car and office, @Autowired will look for beans that match their type, creating a boss bean as part of the boss (car car, office Office).

@autowired annotations and automatic assembly in spring

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.