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

综合网上答案

<context:annotation-config/>

隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
在配置文件中使用<context:annotationconfig/>之前,必须在 <beans> 元素中声明 context 命名空间

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:

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

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

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

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

不过,呵呵,我们使用注解一般都会配置扫描包路径选项

 
 
  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

==================================================================================

Spring家族的配置中这两个配置的意义,说具体点其实根据标签的shecma就能看出来,mvc,主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能,而context这个主要是解决spring容器的一些注解。

  从百度参考了两个帖子:

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

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

 

  

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。

 

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

 
 

在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。

注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下

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

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

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

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

   不过,呵呵,我们使用注解一般都会配置扫描包路径选项

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

    该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。


==================================================================================

 spring中关于<context:component-scan>的使用说明


通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个表情,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean

         注:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用在xml中再配置了,因为前者包含了后者。另外<context:annotation-config/>还提供了两个子标签 <context:include-filter>和 <context:exclude-filter>

在注解注入之前也必须在spring的配置文件中做如下配置,我们看下spring.xml文件的内容:

<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pun">?></span><span class="pln">
</span><span class="tag"><beans</span><span class="pln"> </span><span class="atn">xmlns</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/beans"</span><span class="pln">
       </span><span class="atn">xmlns:xsi</span><span class="pun">=</span><span class="atv">"http://www.w3.org/2001/XMLSchema-instance"</span><span class="pln">
       </span><span class="atn">xmlns:context</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/context"</span><span class="pln">
       </span><span class="atn">xsi:schemaLocation</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd"</span><span class="tag">></span><span class="pln">
	</span><context:component-scan base-package="com.sparta.trans" <span class="atn">use-default-filters</span><span class="pun">=</span><span class="atv">"false"</span><span class="tag"></span>><span class="tag">  </span><pre class="prettyprint prettyprinted" name="code" style="white-space: pre-wrap; word-wrap: break-word; position: relative; overflow-y: hidden; overflow-x: auto;"><pre class="prettyprint prettyprinted" name="code" style="white-space: pre-wrap; word-wrap: break-word; position: relative; overflow-y: hidden; overflow-x: auto;"><span class="pln"></span><span class="tag"></span>              <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><span class="tag"></span><span class="pln"></span><span class="pln">      </span>
<span class="tag">        </context:component-scan></span><span class="pln"></span><span class="pln">
</span>
 
  
</beans> 
 

这个配置文件中必须声明xmlns:context 这个xml命名空间,在schemaLocation中需要指定schema:

<span class="pln">           http</span><span class="pun">:</span><span class="com">//www.springframework.org/schema/context</span><span class="pln">
           http</span><span class="pun">:</span><span class="com">//www.springframework.org/schema/context/spring-context-3.0.xsd</span>
这个文件中beans根节点下只有一个context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含@Service,@Component,@Repository,@Controller注解修饰的类,use-default-filters属性的默认值为true,这就意味着会扫描指定包下标有@Service,@Component,@Repository ,@Controller的注解 的全部类,并注册成bean。所以如果仅仅是在配置文件中写<context:component-scan base-package= "
com.sparta.trans<em>"</em>/> <span style="font-family: FangSong_GB2312;">Use-default-filter此时为true时,那么会对base-package包或者子包下所有的java类进行扫描,并把匹配的java类注册成bean。</span>

 所以这用情况下可以发现扫描的粒度还是挺大的,但是如果你只想扫描指定包下面的Controller,那该怎么办?此时子标签<context:incluce-filter>就可以发挥作用了。如下所示

<context:component-scan base-package="com.sparta.trans.controller">

<pre class="prettyprint prettyprinted" name="code" style="white-space: pre-wrap; word-wrap: break-word; position: relative; overflow-y: hidden; overflow-x: auto;"><span class="tag">        <context:include-filter</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"regex"</span><span class="pln"> </span><span class="atn">expression</span><span class="pun">=</span><span class="atv">"com\.sparta\.trans\.[^.]+(Controller|Service)"</span><span class="tag">/></span>
 

                 <!--  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  -->

</context:component-scan>  

这样就会只扫描base-package指定下的有@Controller下的java类,并注册成bean

注: context:component-scan节点允许有两个子节点<context:include-filter>和<context:exclude-filter>。filter标签的type和表达式说明如下:

 Filter TypeExamples ExpressionDescription
annotationorg.example.SomeAnnotation符合SomeAnnoation的target class
assignableorg.example.SomeClass指定class或interface的全名
aspectjorg.example..*Service+AspectJ語法
regexorg\.example\.Default.*Regelar Expression
customorg.example.MyTypeFilterSpring3新增自訂Type,實作org.springframework.core.type.TypeFilter

在我们的示例中,将filter的type设置成了正则表达式,regex,注意在正则里面.表示所有字符,而\.才表示真正的.字符。我们的正则表示以Controller或者Service结束的类。

我们也可以使用annotaion来限定,如上面注释掉的所示。这里我们指定的include-filter的type是annotation,expression则是注解类的全名。

但是因为use-dafault-filter在上面并没有指定,默认就为true,所以当把上面的配置改成如下所示的时候,就会产生与你期望相悖的结果(注意base-package包值得变化)

<context:component-scan base-package="com.sparta.trans">  

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

</context:component-scan>  

此时,spring不仅扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类

此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。这样就可以避免在base-packeage配置多个包名来解决这个问题了。

另外在实际项目开发中我们可以发现在base-package指定的包中有的子包是不含有注解的,所以不用扫描,此时可以指定<context:exclude-filter>来进行过滤,说明此包不需要被扫描。所以综上可以看出 use-dafault-filters=”false”的情况下:<context:exclude-filter>可以指定不需要扫描的路径来排除扫描这些文件,<context:include-filter>可以指定需要扫描的路径来进行扫描。但是由于use-dafault-filters的值默认为true,所以这一点在实际使用中还是需要注意一下的。


======================================================================================================

spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource、 @PostConstruct及@PreDestroy。

1. @Autowired
    @Autowired是Spring 提供的,需导入
    Package:org.springframework.beans.factory.annotation.Autowired;
    只按照byType 注入。
2. @Resource
    @Resource默认按 byName 自动注入,是J2EE提供的, 需导入Package:  
    javax.annotation.Resource;
    @Resource有两个中重要的属性:name和type ,而Spring将@Resource注解的name属性解析为bean的
    名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用
    type属性时则使用 byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用by
    Name自动注入策略。
 
    @Resource装配顺序 
  (1). 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常;
  (2). 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常;
  (3). 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常;
  (4). 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一
    个原始类型进行匹配,如果匹配则自动装配;
    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入。
3. 使用区别
    @Resource(name="loginService") 
    private LoginService loginService;
 
    @Autowired(required=false)@Qualifier("loginService") 
    private LoginService loginService;
 
   (1).@Autowired 与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上;
   (2).@Autowired 默认按类型装配,默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设

    置它的required属性为false,如:@Autowired(required=false) .

如果我们想使用名称装配可以结合 @Qualifier注解进行使用;

    (3).@Resource(这个注解属于J2EE的),默认安装名称进行装配,名称可以通过name属性进行指定,如果没
    有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属
    性名进行装配。当找不到与名称匹配的bean时才按照类型进行装 配。但是需要注意的是,如果name属性一旦指
    定,就只会按照名称进行装配。
 
    推荐使用@Resource注解在字段上,这样就不用写setter方法了.并且这个注解是属于J2EE的,减少了与Spring

    的耦合,这样代码看起就比较优雅 。

另外,通过实践,还总结出一条规律:

如果将@requied或者@autowired写了set方法之上,则程序会走到set方法内部。

但如果写在了field之上,则不会进入set方法当中。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值