<annotaion-driven/>標簽:
這個標簽對應的實現類是org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
仔細閱讀它的注釋文檔可以很明顯的看到這個類的作用。解析這個文檔:
這個類主要注冊8個類的實例:
1.RequestMappingHandlerMapping
2.BeanNameUrlHandlerMapping
3.RequestMappingHandlerAdapter
4.HttpRequestHandlerAdapter
5.SimpleControllerHandlerAdapter
6.ExceptionHandlerExceptionResolver
7.ResponseStatusExceptionResolver
8.DefaultHandlerExceptionResolver
1是處理@RequestMapping注解的,2.將controller類的名字映射為請求url。1和2都實現了HandlerMapping接口,用來處理請求映射。
3是處理@Controller注解的控制器類,4是處理繼承HttpRequestHandlerAdapter類的控制器類,5.處理繼承SimpleControllerHandlerAdapter類的控制器。所以這三個是用來處理請求的。具體點說就是確定調用哪個controller的哪個方法來處理當前請求。
6,7,8全部繼承AbstractHandlerExceptionResolver,這個類實現HandlerExceptionResolver,該接口定義:接口實現的對象可以解決處理器映射、執行期間拋出的異常,還有錯誤的視圖。
所以<annotaion-driven/>標簽主要是用來幫助我們處理請求映射,決定是哪個controller的哪個方法來處理當前請求,異常處理。
<context:component-scan/>標簽:
它的實現類是org.springframework.context.annotation.ComponentScanBeanDefinitionParser.
把鼠標放在context:component-scan上就可以知道有什么作用的,用來掃描該包內被@Repository @Service @Controller的注解類,然后注冊到工廠中。並且context:component-scan激活@ required。@ resource,@ autowired、@PostConstruct @PreDestroy @PersistenceContext @PersistenceUnit。使得在適用該bean的時候用@Autowired就行了。
本文開門見山,直接分別進行解釋:
一、<context:annotation-config/>
隱式地向spring容器中注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 這 4 個 BeanPostProcessor
對這個結果類做個解釋:
1、如果你想使用@Autowired注解,那么就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。
2、如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必須聲明CommonAnnotationBeanPostProcessor。
3、如果想使用@PersistenceContext注解,就必須聲明PersistenceAnnotationBeanPostProcessor的Bean。
4、如果想使用 @Required的注解,就必須聲明RequiredAnnotationBeanPostProcessor的Bean。
分別對應的傳統聲明方式為:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
一般來說,這些注解我們還是比較常用,尤其是Antowired的注解,在自動注入的時候更是經常使用,所以如果總是需要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,於是spring給我們提供 <context:annotation-config>
的簡化配置方式,自動幫你完成聲明。
<context:annotation-config/>
將隱式地向 Spring 容器注冊 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 這 4 個 BeanPostProcessor。
二、 <context:component-scan/>
<context:component-scan/>
配置項不但啟用了對類包進行掃描以實施注釋驅動 Bean 定義的功能,同時還啟用了注釋驅動自動注入的功能(即還隱式地在內部注冊了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此當使用<context:component-scan base-package="xxx.xxx.xxx"/>
后,就可以將 <context:annotation-config/>
移除了。
默認情況下通過 @Component 定義的 Bean 都是 singleton 的,如果需要使用其它作用范圍的 Bean,可以通過 @Scope 注釋來達到目標,如:@Scope(“prototype”),這樣,當從 Spring 容器中獲取 boss Bean 時,每次返回的都是新的實例了。
三、<mvc:annotation-driven/>
相當於注冊了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,配置一些messageconverter。即解決了@Controller注解的使用前提配置。
官方文檔中也進行了說明,如下: <mvc:annotation-driven/>
is a tag added in Spring 3.0 which does the following:
- Configures the Spring 3 Type ConversionService (alternative to PropertyEditors)
- Adds support for formatting Number fields with @NumberFormat
- Adds support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
- Adds support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
- Adds support for support for reading and writing XML, if JAXB is on the classpath (HTTP message conversion with @RequestBody/@ResponseBody)
-
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