• <context:annotation-config/>

在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<context:annotation-config/>这样一条配置,它的作用是隐式的向Spring容器注册

                          
AutowiredAnnotationBeanPostProcessor,
                           CommonAnnotationBeanPostProcessor,
                          
PersistenceAnnotationBeanPostProcessor,
                           RequiredAnnotationBeanPostProcessor 
 这4个BeanPostProcessor.注册这4个bean处理器主要的作用是为了你的系统能够识别相应的注解。

例如:

  1. 如果想使用@Autowired注解,需要在Spring容器中声明AutowiredAnnotationBeanPostProcessor
    Bean。传统的声明方式:<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  2. 如果想使用@PersistenceContext注解,需要在Spring容器中声明PersistenceAnnotationBeanPostProcessor
    Bean。传统的声明:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  3. 如果想使用@Required注解,需要在Spring容器中声明RequiredAnnotationBeanPostProcessor
    Bean。传统声明方式: <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  4. 如果想使用@Resource、@ PostConstruct、@
    PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。传统申明方式:
    <bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>

所以,如果按照传统声明一条一条去声明注解Bean,就会显得十分繁琐。

因此如果在Spring的配置文件中事先加上<context:annotation-config/>这样一条配置的话,那么所有注解的传统声明就可以被 
忽略,即不用在写传统的声明,Spring会自动完成声明。

  • <context:component-scan base-package="com.xx"
    />

<context:component-scan/>的作用是让Bean定义注解工作起来,也就是上述传统声明方式。
它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。


    
值得注意的是<context:component-scan/>不但启用了对类包进行扫描以实施注释驱动
Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor
和  CommonAnnotationBeanPostProcessor),因此当使用
<context:component-scan/>
后,就可以将 <context:annotation-config/>
移除了。

 
 @Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。注解之后就不需要set/get方法了。
 

[转载]Spring配置文件详解一:的更多相关文章

  1. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  2. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  3. J2EE进阶(四)Spring配置文件详解

    J2EE进阶(四)Spring配置文件详解 前言 Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程 ...

  4. spring配置文件详解以及beans:beans标签

    第一行的意思就是你这个文件的默认schema为security,所以你的beans定义就需要加上前缀beans 一般的定义文件默认都是beans: 下面是spring配置文件的详解: 转自:http: ...

  5. Spring 配置文件详解 (以2.5为例)

    转载自:http://blog.csdn.net/zzjjiandan/article/details/22922847          Spring配置文件是用于指导Spring工厂进行Bean生 ...

  6. Spring配置文件详解

      转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...

  7. Spring配置文件详解 - applicationContext.xml文件路径

    spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...

  8. Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />

    <context:annotation-config/> 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<contex ...

  9. 【 spring配置文件详解】

    转自: http://book.51cto.com/art/201004/193743.htm Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的 ...

随机推荐

  1. java微信开发API解析(四)-自己定义菜单以及个性化菜单实现

    全局说明 * 具体说明请參考前两篇文章. 本文说明 *本文分为五部分: * 工具类AccessTokenUtils的封装 * 自己定义菜单和个性化菜单文档的阅读解析 * 菜单JSON的分析以及构建相应 ...

  2. shell 命令getopts用法

    写shell脚本常见sh test.sh -m 2 -d 3的写法 事例脚本: #!/bin/bash while getopts ":a:b:c:" arg #选项后面的冒号表示 ...

  3. mysql的体系架构和存储引擎

    定义数据库和实例 数据库:物理操作系统的文件或其他形式文件类型的集合.在mysql数据库中,数据库文件可以是frm.MYD.MYI.ibd结尾的文件. 实例:MySQL数据库由后台线程以及一个共享内存 ...

  4. python学习【第六篇】python迭代器与生成器

    一.什么是迭代器 迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代(只能往后走不能往前退) 可迭代对象:实现了迭代器 ...

  5. java 多种判断key是否在map中存在的方法

    java 中有时候会遇到判断传过来的map里是否包含了指定的key,我目前只发现两种办法,如果有其他方法欢迎补充 我添加上去: HashMap map = new HashMap(); map.put ...

  6. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. JSP页面中的tab页

    首先下载bootstrap-tab.js和bootstrap.min.css <script type="text/javascript">$('#myTab a'). ...

  8. Power Systems 虚拟化简介

    本文向您详细地介绍了 Power System 虚拟化相关的技术和亮点,让您对这些最新的虚拟化技术有一个全面的了解.本文来自 IBM Systems Magazine for AIX 中文版. 自从引 ...

  9. js实现文字列表无缝向上滚动

    body{font-size:12px} #demo{overflow:hidden; height:80px; width:280px; margin:90px auto; position:rel ...

  10. 洛谷 P1640 [SCOI2010]连续攻击问题

    洛谷 一句话题意: 每个武器有两种属性,每种武器只能选择一种属性,从属性1连续递增才算攻击,求最大连续攻击次数. 因为同学告诉我这是二分图最大匹配,自然就往那个方向去想. 那么怎么建图呢? 每个武器只 ...