Starting with spring 2.5, the functionality of a fully annotation-based configuration bean, assembly bean is provided. We can replace the original XML-based configuration with a note-based Spring IoC. Previously done projects using XML configuration to achieve, these days in a small project, the use of SPRINGMVC annotations, to share with you.
Using annotations
Spring 2.5 provides the autowiredannotationbeanpostprocessor,
Commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor and Requiredannotationbeanpostprocessor These are the four main beanpostprocessor about Annotation.
We can use <context:annotation-config/> to conveniently, one-time declarator four beanpostprocessor.
1. Autowired ... Provides autowired and Qualifier annotations that are unique to Spring.
2. Commonanotation ... Annotations to support JSR 250
3. Persistence ... Persistenceunit and persistencecontext annotations for JPA
4. Required ... Used to check if the property of the Required comment tag is set.
Originally we configured the XML
Before using the annotation configuration, let's review how the Bean is traditionally configured and complete the creation of dependencies between beans. Here are 3 classes, which are Office, Car, and Boss, and these 3 classes need to be configured as beans in a Spring container:
Package Com.baobaotao; public class Office { private String Officeno = "001"; Omit Get/setter @Override public String toString () { return "Officeno:" + Officeno;
public class Car { private String brand; private double price; Omit Get/setter @Override public String toString () { return ' Brand: ' + brand + ', ' + ' Price: ' + price;
} }
Package Com.baobaotao; 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: The following is the configuration file Beans.xml, which accomplishes this work using traditional 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 ">
<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= "$"/>
</beans>
Using @autowired
Spring 2.5 introduces @Autowired annotations that allow you to annotate class member variables, methods, and constructors, and complete the task of automating Assembly. Take a look at the code that uses @Autowired to automatically inject member variables.
public class Boss {
@Autowired
Private car car;
@Autowired
Private Office Office;
}
Spring parses the @Autowired through a beanpostprocessor, so that @Autowired must be The Autowiredannotationbeanpostprocessor Bean is declared in the container.
Let @autowired work.
<?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 The beanpostprocessor will automatically function 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= "$"/>
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.
Annotation configuration vs. XML configuration
1, which can take full advantage of the Java reflection mechanism to obtain class structure information, this information can effectively reduce the work of the configuration. When configuring an ORM map with JPA annotations, we do not need to specify the property name, type, etc. of the PO, and you do not even need to write task property mapping information if the Relationship table fields and PO property names and types are identical-because this information can be obtained through the Java reflection mechanism.
2, annotations, and Java code are located in a single file, and the XML configuration takes a separate configuration file, and most of the configuration information is not adjusted after the program is developed, and if the configuration information and Java code are put together, it helps to enhance the cohesion of the program. In the case of a standalone XML configuration file, programmers often need to switch between program files and configuration files when writing a function, and this incoherent thinking can reduce development efficiency.
Therefore, in many cases, the annotation configuration is more popular than the XML configuration, and the annotation configuration has a tendency to be more popular. One of the big enhancements to Spring 2.5 is the introduction of a number of annotation classes, which you can now use to configure most of the XML configuration. In this article, we will tell you about the use of annotations for Bean definitions and dependency injection.
Concrete analysis of specific problems
Summary: The above comparison is a theoretical comparison, do project practice to realize that each project in the end XML configuration or use annotations to configure, with the size of the project, the demand has a great relationship, not "an annotation is simpler than the XML" to use, or need specific problems specific analysis.
Spring MVC Annotations