annotation-config, annotation-driven, compont-scan 区别

Reprinted from: http://www.developersite.org/903-35760-spring

 

Comprehensive online answers

<context:annotation-config/>

The four BeanPostProcessors, AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenteAnnotationBeanPostProcessor and requiredAnnotationBeanPostProcessor , are implicitly registered in the Spring container .
Before using <context:annotationconfig/> in a configuration file, the context namespace must be declared in the <beans> element

E.g:

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

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

If you want to use annotations such as @Resource, @PostConstruct, @PreDestroy, 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:

  1. <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

  1. <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.

 

The <context:component-scan/> 
configuration item not only enables the scanning of class packages to implement annotation-driven bean definitions, but also enables annotation-driven automatic injection (that is, it also implicitly registers AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor internally. ), so when using <context:component-scan/>, you can remove <context:annotation-config/> unless you need to use the functions of PersistenceAnnotationBeanPostProcessor and requiredAnnotationBeanPostProcessor (such as JPA, etc.)

 

< mv c:annotation-driven/>
is equivalent to registering two beans, DefaultAnnotationHandlerMap ping and AnnotationMethodHandlerAdapter, and configuring some messageconverters. That is,the prerequisite configuration of the @Contr oller annotation is solved.

<mvc:annotation-driven/> is a tag added in Spring 3.0 which does the following: 

1. Configures the Spring 3 Type ConversionService (alternative to PropertyEditors) 
2. Adds support for formatting Number fields with @NumberFormat 
3. Adds support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath 
4. Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath 
5. Adds support for support for reading and writing XML, if JAXB is on the classpath (HTTP message conversion with @RequestBody/@ResponseBody) 
6. Adds support for reading and writing JSON, if Jackson is o n the classpath (along the same lines as #5) 

<context:annotation-config/> 
Looks for annotations on beans in the same application context it is defined and declares support for all the general annotations like @Autowired, @Resource, @Required, @PostConstruct etc etc. 

<context:annotation-config> does NOT search for @Component, @Controller, etc. 
<context:component-scan> DOES search for those @Component annotations, as well as the annotations that <context:annotation-config/> does.

there are other "annotation-driven" tags available to provide additional functionality in other Spring modules. For example, <transaction:annotation-driven /> enables the use of the @Transaction annotation, <task:annotation-driven /> is required for @Scheduled et al

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326475083&siteId=291194637