天天看点

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

本文开门见山,直接分别进行解释:

一、<code>&lt;context:annotation-config/&gt;</code>

隐式地向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。

分别对应的传统声明方式为:

一般来说,这些注解我们还是比较常用,尤其是antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供 <code>&lt;context:annotation-config&gt;</code> 的简化配置方式,自动帮你完成声明。

<code>&lt;context:annotation-config/&gt;</code> 将隐式地向 spring 容器注册 autowiredannotationbeanpostprocessor、commonannotationbeanpostprocessor、persistenceannotationbeanpostprocessor 以及 equiredannotationbeanpostprocessor 这 4 个 beanpostprocessor。

二、 <code>&lt;context:component-scan/&gt;</code>

<code>&lt;context:component-scan/&gt;</code> 配置项不但启用了对类包进行扫描以实施注释驱动 bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 autowiredannotationbeanpostprocessor 和 commonannotationbeanpostprocessor),因此当使用 <code>&lt;context:component-scan base-package="xxx.xxx.xxx"/&gt;</code> 后,就可以将 <code>&lt;context:annotation-config/&gt;</code> 移除了。

默认情况下通过 @component 定义的 bean 都是 singleton 的,如果需要使用其它作用范围的 bean,可以通过 @scope 注释来达到目标,如:@scope(“prototype”),这样,当从 spring 容器中获取 boss bean 时,每次返回的都是新的实例了。

三、<code>&lt;mvc:annotation-driven/&gt;</code>

相当于注册了defaultannotationhandlermapping和annotationmethodhandleradapter两个bean,配置一些messageconverter。即解决了@controller注解的使用前提配置。

官方文档中也进行了说明,如下:

<code>&lt;mvc:annotation-driven/&gt;</code> 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)

<code>&lt;context:annotation-config/&gt;</code>

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.

<code>&lt;context:annotation-config&gt;</code> does not search for @component, @controller, etc.

<code>&lt;context:component-scan&gt;</code> does search for those @component annotations, as well as the annotations that <code>&lt;context:annotation-config/&gt;</code> does.

there are other “annotation-driven” tags available to provide additional functionality in other spring modules. for example, <code>&lt;transaction:annotation-driven /&gt;</code> enables the use of the @transaction annotation, <code>&lt;task:annotation-driven /&gt;</code> is required for @scheduled et al