Spring mvc Annotation

Source: Internet
Author: User

Spring mvc Annotation

Starting from Spring 2.5, Spring provides the function of completely configuring beans based on Annotations and assembling beans. We can use annotation-based Spring IoC to replace the original XML-based configuration. Previously, the project was implemented using xml configuration. In the past few days, a small project was built and springmvc annotations were used to share with you.

Annotations

Spring 2.5 provides AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, and RequiredAnnotationBeanPostProcessor are the four major Annotation BeanPostProcessor.
We can use BeanPostProcessor.
1. Autowired... provides notes for Spring-specific Autowired and Qualifier.

2. CommonAnotation... used to support comments of JSR 250
3. Persistence... used for JPA PersistenceUnit and PersistenceContext annotations

4. Required... used to check whether the attribute marked by the Required annotation is set.

 

We configured xml
Before using annotation configuration, let's review how to configure beans and establish dependencies between beans. The following are three classes: Office, Car, and Boss. These three classes need to be configured as Bean in the 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 ++ office: + office;
} }

 

We declare Office and Car as beans in the Spring container and inject them into the Boss Bean. below is the configuration file beans. XML that uses traditional xml to complete this work.

   
 

 

Use @ autowired


Spring 2.5 introduces @ Autowired annotations, which can mark class member variables, methods, and constructors to complete automatic assembly. Let's take a look at the code that uses @ Autowired to automatically inject member variables.

import org.springframework.beans.factory.annotation.Autowired; 
public class Boss {     
@Autowired     
private Car car;     
@Autowired     
private Office office;
}

Spring parses @ Autowired through a BeanPostProcessor, so to make @ Autowired work, you must declare AutowiredAnnotationBeanPostProcessor Bean in the Spring container in advance.
Let @ Autowired work

  
      
       
  
   
AutowiredAnnotationBeanPostProcessor/>    
 
         
    
             
    
         
    
     
             
    
             
    
         
    
 
 

 

In this way, when the Spring container is started, AutowiredAnnotationBeanPostProcessor will scan all the beans in the Spring container, and find the Bean that matches with the @ Autowired annotation in the Bean (by default, by type, and inject it to the corresponding place.
According to the above configuration, Spring will directly use the Java reflection mechanism to automatically inject the private member variables car and office in the Boss.

 

Comparison between annotation configuration and XML configuration


?? 1. It can take full advantage of the Java reflection mechanism to obtain the class structure information, which can effectively reduce the configuration work. For example, when using JPA annotations to configure ORM ing, we do not need to specify the attribute name, type, and other information of the PO. If the Link Table field and the PO attribute name and type are consistent, you don't even need to write the task property ing information-because this information can be obtained through the Java reflection mechanism.


?? 2. Annotations and Java code are located in one file. XML configuration adopts an independent configuration file. Most configuration information is not adjusted after the program is developed, if the configuration information is put together with the Java code, it helps enhance the cohesion of the program. When using an independent XML configuration file, programmers often need to switch between program files and configuration files when writing a function. This inconsistency of thinking will reduce the development efficiency.
Therefore, in many cases, annotation configuration is more popular than XML configuration, and annotation configuration is more popular. A major enhancement of Spring 2.5 is the introduction of many annotation classes. Now you can use annotation configuration to complete most of the XML configuration functions. In this article, we will introduce you to Bean definition and dependency Injection Using annotations.

 

Specific Problem Analysis

Conclusion: The comparison above is a theoretical comparison. After project practice, I realized that the xml configuration of each project is still configured using annotations, which has a great relationship with the project size and requirements, it is not necessary to use an annotation that is simpler than XML, but it still needs to be analyzed in detail.

??

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.