关于Spring中的context:annotation-config/配置(开启注解)
转自:https://www.cnblogs.com/doudouxiaoye/p/5681518.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>
转载于:https://www.cnblogs.com/sharpest/p/10020487.html
关于Spring中的context:annotation-config/配置(开启注解)相关推荐
- Spring 中的 context
Spring 中的 context BeanFactory 首先看下,官方在代码中给出的注释: The root interface for accessing a Spring bean conta ...
- spring中基于XML的AOP配置步骤
spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...
- Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式
文章目录 Pre 实现 方式一 通过 shared-dataids 方式 方式二 通过 ext-config方式 配置文件优先级 源码 Pre Spring Cloud Alibaba - 18 Na ...
- spring tx:advice 和 aop:config 配置事务
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010741376/article/details/46584463 spring tx:advic ...
- spring 加载java类_在Spring中基于Java类进行配置的完整步骤
在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...
- Spring Cloud Alibaba - 18 Nacos Config配置中心加载相同微服务的不同环境下的通用配置
文章目录 需求 实现 Step 1 Nacos Config 新增公共配置 Step 2 验证 配置文件优先级 源码 需求 举个例子,同一个微服务,通常我们的servlet-context 都是相同的 ...
- java快速注释怎么配置_详解如何在低版本的Spring中快速实现类似自动配置的功能...
在 Spring 4 后才引入了 @Conditional 等条件注解,它是 Spring Boot 中实现自动配置的最大功臣! 那么问题来了:如果我们还在使用 Spring 3.x 的老版本,这时候 ...
- Spring中定时任务Quartz集群配置学习
Spring中定时任务Quartz集群配置学习 原来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运行,没 ...
- vue-cli中的webpack的config配置详细说明
vue-cli中的webpack的config配置详细说明 打开我们的vue项目文件夹我们可以清楚的看到文件夹下的三个文件, "dev.env.js","index.js ...
- Spring中的packagesToScan的方式配置hibernate的class文件映射规则的理解
Spring中的packagesToScan的方式配置hibernate的class文件映射规则的理解 1.持久化实体,使用自动扫描class的形式进行配置时,规则如下 (1)& ...
最新文章
- python递归合并排序_python 归并排序的递归法与迭代法(利用队列)实现,以及性能测试...
- 每日一皮:这就是成都马拉松???...
- 07/11/20 资料整理
- 多溴联苯醚内分泌干扰效应机制研究取得进展
- C#的类,构造函数以及Array阵列的数据填充与绑定
- 使用pip安装BeautifulSoup4模块
- Linux学习笔记Sed最全整理
- php cookie赋值使用
- error: expected expression before ‘;’ token的问题
- CentOS 6.2 中文
- 模拟按键——在Python使用pynput库
- Android Studio如何隐藏系统的状态栏
- 用Adomd.net构建最清凉的olap示例
- Unix/Linux环境C编程新手教程(40) 初识文件操作
- Lookup 组件用法全解
- iOS切换window根控制器
- Linux基础课程汇总-辛舒展-专题视频课程
- Nginx专题:nginx+tomcat实现动静分离
- sql统计表个数以及表中字段个数
- python中词云图是用来描述_Python如何实现中国地图词云图