转自:https://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html

当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
  使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

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

  使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

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

  类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation-
config/>隐式地向
Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:

<context:annotation-config/> 

  另,在我们使用注解时一般都会配置扫描包路径选项:

<context:component-scan base-package="pack.pack"/>

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

备注:
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
...
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
...
<context:annotation-config/>
</beans>

关于Spring中的<context:annotation-config/>配置作用的更多相关文章

  1. Spring - IoC(8): 基于 Annotation 的配置

    除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...

  2. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  3. 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法

    Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法    获取Class类实例的方法:   类名.class   实例名.getClass()   Class.forNam ...

  4. 关于Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  5. 关于Spring中的<context:annotation-config/>配置(开启注解)

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  6. Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  7. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  8. spring中关于<context:component-scan>的使用说明(转)

    https://blog.csdn.net/liuxingsiye/article/details/52171508 通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个表情,配置 ...

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

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

随机推荐

  1. 安卓手机USB无法共享、上网或卡顿的解决方法

    安卓手机通过USB为电脑(Windows10)提供网络接入点时,系统程序会异常卡顿. 1)设备管理器2)点击“网络适配器”,在弹出的下拉列表中选择”Remote NDIS based Internet ...

  2. PyCharm 恢复默认设置 | JetBrains IDE 配置文件安装目录

    网上的答案为什么都乱七八糟并且全都全篇一律?某度知道是发源地? 先说 Mac 按需运行下面的 rm 删除命令 # Configuration rm -rf ~/Library/Preferences/ ...

  3. kipmi0进程单核CPU100%的解决办法

    top查看服务器进程,发现有个kipmi0的进程竟然CPU的单核占用高达100%,而且居高不下. 于是上网搜了搜大家的说法了给出的链接,大概意思是一个固件问题,可以通过修改文件来解决. 专业的解释地址 ...

  4. lvs负载均衡net模式

    环境配置,一台双网卡的ens33,ens37,ens37的网关是ens33的IP,指定一下nginx ens33,192.168.30.22,ens37,172.16.1.1nginx  192.16 ...

  5. 【airtest, python】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下

    环境及设备 mac, xcode , iphonex 问题 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connectio ...

  6. virtualenv技巧

    看了你就懂了 https://segmentfault.com/a/1190000004079979 你在一个机器上 开发不通的项目,需要不通版本的python怎么办 1.首先安装python3 ht ...

  7. HTTP 状态码之:301、302 重定向

    转自:http://www.cnblogs.com/5207/p/5908354.html 概念 301 Moved Permanently 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用 ...

  8. 计蒜客 奇异家庭 (DP)

    链接 : Here! 思路 : 首先这棵家族树非常非常非常有特点, 家族里的人要么没有孩子, 要么有两个孩子, 所以这棵家族树是一颗满二叉树. 设定状态 $dp[i][j]$ 为 $i$ 个人组成的不 ...

  9. docker安装部署

    1. 如何安装 Epel源到 RHEL/CentOS 7/6/5? RHEL/CentOS rpm -ivh http://mirrors.ustc.edu.cn/epel/7/x86_64/Pack ...

  10. 09.正则表达式re-1.正则表达式

    1.正则表达式概述 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念. 正则表达式使用单个字符串来描述.匹配一系列匹配某 ...