Spring's annotation details

<mvc:annotation-driven />与<context:annotation-config />

 

  The meaning of these two configurations in the configuration of the Spring family can be seen from the shecma of the label. Mvc is mainly used for Spring MVC, providing functions such as Controller request forwarding, json automatic conversion, etc., and the context this Mainly to solve some annotations of the spring container.

  Referenced two posts from Baidu:

    http://blog.csdn.net/sxbjffsg163/article/details/9955511

    http://blog.sina.com.cn/s/blog_872758480100wtfh.html

 

  

<mvc:annotation-driven /> is a shorthand form, which can be completely replaced by manual configuration. The shorthand form allows beginners to quickly apply the default configuration scheme. <mvc:annotation-driven /> will automatically register two beans, DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter, which are necessary for spring MVC to distribute requests to @Controllers. (Configure spring mvc must configure this annotation or the request will fail)
and provides: data binding support, @NumberFormatannotation support, @DateTimeFormat support, @Valid support, support for reading and writing XML (JAXB), support for reading and writing JSON (Jackson ).
Later, when we respond to ajax requests, we use support for json.
Later, when writing a JUnit unit test for an action, you need to take the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans from the spring IOC container to complete the test. When taking it, you must know that the two beans registered by the sentence <mvc:annotation-driven /> .

 

<context:annotation-config> declares support for general annotations such as @Required@Autowired@PostConstruct, and so on.

<mvc:annotation-driven /> is actually rather pointless. It declares explicit support for annotation-driven MVC controllers (i.e.@RequestMapping@Controller, etc), even though support for those is the default behaviour.

My advice is to always declare <context:annotation-config>, but don't bother with <mvc:annotation-driven /> unless you want JSON support via Jackson.

 
 

In the configuration file for configuring Spring based on the host method , you may see a configuration such as <context:annotation-config/>, which is used to  register with the Spring container.

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor and  RequiredAnnotationBeanPostProcessor  are four BeanPostProcessors.

The purpose of registering these four BeanPostProcessors is for your system to recognize the corresponding annotations. 

E.g:

If you want to use the @Autowired annotation, you must declare  the AutowiredAnnotationBeanPostProcessor bean in the  Spring container in advance. The traditional way of declaration is as follows

<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 

If you want to use annotations such as @Resource, @PostConstruct , @PreDestroy, etc., you must declare CommonAnnotationBeanPostProcessor

If you want to use the @PersistenceContext annotation, you must declare the PersistenceAnnotationBeanPostProcessor bean.

If you want to use  the @Required annotation, you must declare the Bean of RequiredAnnotationBeanPostProcessor. Again, the traditional way of declaring is as follows:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

Generally speaking, these annotations are still commonly used, especially the annotations of Antowired, which are often used during automatic injection, so if it is always necessary to configure one by one in the traditional way, it is a bit cumbersome and unnecessary, so spring gives us Provides a simplified configuration method of <context:annotation-config/> to automatically complete the declaration for you.

   However, huh, we use annotations to generally configure scan package path options

<context:component-scan base-package=”XX.XX”/> 

    This configuration item actually includes the function of automatically injecting the above processor, so when using  <context:component-scan/>,  <context:annotation-config/> can be removed.

 

Article reference: http://www.cnblogs.com/dreamroute/p/4493346.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326844939&siteId=291194637