annotation-config, annotation-driven, compont-scan 區別

綜合網上答案html

<context:annotation-config/>
java

隱式地向spring容器中註冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 這 4 個 BeanPostProcessor。
在配置文件中使用<context:annotationconfig/>以前,必須在 <beans> 元素中聲明 context 命名空間
linux

例如:spring

若是你想使用@Autowired註解,那麼就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。傳統聲明方式以下:mvc

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

若是想使用@ Resource 、@ PostConstruct、@ PreDestroy等註解就必須聲明CommonAnnotationBeanPostProcessorapp

若是想使用@PersistenceContext註解,就必須聲明PersistenceAnnotationBeanPostProcessor的Bean。ide

若是想使用 @Required的註解,就必須聲明RequiredAnnotationBeanPostProcessor的Bean。一樣,傳統的聲明方式以下:ui

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

通常來講,這些註解咱們仍是比較經常使用,尤爲是Antowired的註解,在自動注入的時候更是常常使用,因此若是老是須要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,因而spring給咱們提供<context:annotation-config/>的簡化配置方式,自動幫你完成聲明。spa

不過,呵呵,咱們使用註解通常都會配置掃描包路徑選項.net

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

該配置項其實也包含了自動注入上述processor的功能,所以當使用 <context:component-scan/> 後,就能夠將 <context:annotation-config/> 移除了。

 

<context:component-scan/>
配置項不但啓用了對類包進行掃描以實施註釋驅動 Bean 定義的功能,同時還啓用了註釋驅動自動注入的功能(即還隱式地在內部註冊了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),所以當使用 <context:component-scan/> 後,除非須要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor兩個Processor的功能(例如JPA等)不然就能夠將 <context:annotation-config/> 移除了

 

<mvc:annotation-driven/>
至關於註冊了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,配置一些messageconverter。即解決了@Controller註解的使用前提配置。

<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

相關文章
相關標籤/搜索