关于Spring中的<context:annotation-config/>配置(开启注解)
当我们需要使用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/>配置(开启注解)的更多相关文章
- Spring - IoC(8): 基于 Annotation 的配置
除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法 获取Class类实例的方法: 类名.class 实例名.getClass() Class.forNam ...
- 关于Spring中的<context:annotation-config/>配置
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...
- 关于Spring中的<context:annotation-config/>配置作用
转自:https://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html 当我们需要使用BeanPostProcessor时,直接在Spri ...
- Spring中的<context:annotation-config/>配置
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...
- spring cloud 2.x版本 Config配置中心教程
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...
- spring中关于<context:component-scan>的使用说明(转)
https://blog.csdn.net/liuxingsiye/article/details/52171508 通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个表情,配置 ...
- spring中关于<context:component-scan>的使用说明
通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...
随机推荐
- Qt 滚动窗口类
{ QScrollArea *scrollArea = new QScrollArea(this); scrollArea->setFrameStyle(); scrollArea->se ...
- elixir东游记:实现一个简单的中文语句解析
备份:https://zhuanlan.zhihu.com/p/46030123 代码地址:github:pyzh/gdpl-ex.poc-1 原语句是:List1为'12332234':记a为Lis ...
- PHP多进程的实际处理
多进程应用大批量的数据是非常舒服的一件事情. 处理之前理解两个概念:孤儿进程和僵尸进程 孤儿进程: 是指父进程在fork出子进程后,自己先完了.这个问题很尴尬,因为子进程从此变得无依无靠.无家可归,变 ...
- 考研计算机复试笔试(数据结构/C语言简答题篇)
1.比较顺序存储结构和链式存储结构的优缺点,什么情况下链表比顺序表好? 顺序存储时相邻元素的存储单元的地址也相连,可以随机存取.优点是存储密度大,空间利用率高:缺点是插入或删除时不方便. 链式存储时相 ...
- python笔记21-内置函数
# print(all([1,2,3,4]))#判断可迭代的对象里面的值是否都为真# print(any([0,0,0,0,0]))#判断可迭代的对象里面的值是否有一个为真# print(bin(10 ...
- leetcode日记 HouseRobber I II
House Robber I You are a professional robber planning to rob houses along a street. Each house has a ...
- 数列全排列问题----递归实现--JAVA
public class PaiLie { /** * @param args */ public static void main(String[] args) { PaiLie p=new Pai ...
- windows上传文件到linux云服务器上
安装putty,将pscp.exe移到 C:\Windows\System32 目录下. 在cmd 中执行,pscp -l rot -pw [password] -ls [ip]:/opt 查看目录 ...
- Beta冲刺 6
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10129063.html 作业博客:https://edu.cnblogs.com/campus ...
- 将一个float型转化为内存存储格式的步骤
将一个float型转化为内存存储格式的步骤为: (1)先将这个实数的绝对值化为二进制格式. (2)将这个二进制格式实数的小数点左移或右移n位,直到小数点移动到第一个有效数字的右边. (3)从小数点右边 ...