一、容器后处理器

  1. Spring有如下几个常用容器后处理器:
    PropertyPlaceholderConfigurer:属性点位符配置器
    PropertyOverrideConfigurer:重写占位符配置器
    CustomAutowireConfigurer:自定义自动装配的配置器
    CustomScopeConfigurer:自定义作用域的配置器

  2. 容器后处理器用于负责处理容器本身,须实现BeanFactoryPostProcessor接口
    并实现该接口的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法。

  3. 如果采用ApplicationContext作为Spring容器,会自动注册容器后处理器,如果采用BeanFactory作为Spring容器,则需要手动调用该容器来处理Spring容器。

  4. 代码示例:
    bean.xml配置文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="user" class="com.kting.agx.test.User"/></beans>

Java代码:

//容器后处理器
public class MyClass implements BeanFactoryPostProcessor{   @Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException {System.out.println("这是容器后处理器,名字是:"+beanFactory);}/*** 测试*/public static void main(String[] args) {//采用ApplicationContext作为Spring容器ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");User user = (User) app.getBean("user");//"user"参数是在bean.xml里注入User类时的iduser.getMessage();}}

如果用BeanFactory作为Spring容器,则需要在bean.xml里配置MyClass类,即手动注册容器后处理器:

<bean id="myId" class="com.kting.agx.test.MyClass"/>

二、属性占位符配置器:PropertyPlaceholderConfigurer

该容器后处理器负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文年的元数据,例如数据的URL、用户名、密码等等…..

base-conf-local.properties文件代码:

com.kting.agx.base.dataSource0.jdbc.driverClassName=com.mysql.jdbc.Driver
com.kting.agx.base.dataSource0.jdbc.url=jdbc:mysql://localhost:3306/agx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
com.kting.agx.base.dataSource0.jdbc.username=agx
com.kting.agx.base.dataSource0.jdbc.password=agx987654com.kting.agx.base.dataSource1.jdbc.driverClassName=com.mysql.jdbc.Driver
com.kting.agx.base.dataSource1.jdbc.url=jdbc:mysql://localhost/agx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
com.kting.agx.base.dataSource1.jdbc.username=agx
com.kting.agx.base.dataSource1.jdbc.password=agx987654

bean.xml配置文件代码:

<!-- 这样配置后,可通过${}形式获取前面properties文件里的值 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>base-conf-local.properties</value><!-- 如果有多个配置文件,可依次列出来 : --><!-- <value>base-conf-online.properties</value> --></list></property>
</bean>
<bean id="write_dataSource" class="com.alibaba.druid.pool.DruidDataSource"parent="commonDataSourceProperties" init-method="init" destroy-method="close"><!-- 基本属性 url、user、password --><property name="url" value="${com.kting.agx.base.dataSource0.jdbc.url}" /><property name="username" value="${com.kting.agx.base.dataSource0.jdbc.username}" /><property name="password" value="${com.kting.agx.base.dataSource0.jdbc.password}" />
</bean><bean id="read_dataSource" class="com.alibaba.druid.pool.DruidDataSource"parent="commonDataSourceProperties" init-method="init" destroy-method="close"><!-- 基本属性 url、user、password --><property name="url" value="${com.kting.agx.base.dataSource1.jdbc.url}" /><property name="username" value="${com.kting.agx.base.dataSource1.jdbc.username}" /><property name="password" value="${com.kting.agx.base.dataSource1.jdbc.password}" />
</bean>

如果在bean.xml文件的头部分导入了context Schema:

xmlns:context="http://www.springframework.org/schema/context"

则我们可通过如下方式配置该属性占位符:

<context:property-placeholder location="classpath:base-conf-local.properties" />

如果有多个properties文件时,可用通配符的形式

<context:property-placeholder location="classpath*:*-conf-local.properties" />

三、重写占位符配置器:PropertyOverrideConfigurer

Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)相关推荐

  1. spring开发_BeanFactoryPostProcessor_容器后处理器

    项目结构: http://www.cnblogs.com/hongten/gallery/image/112611.html /spring_1700_容器后处理器/src/com/b510/app/ ...

  2. BeanFactoryPostProcessor接口(容器后处理器)

    2019独角兽企业重金招聘Python工程师标准>>> 容器后处理器后处理容器本身.必须实现BeanFactoryPostProcessor接口,该接口中有如下方法: postPro ...

  3. Java框架篇---spring aop两种配置方式

    Java框架篇---spring aop两种配置方式 第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:  1. 使用注解@Aspect来定义一个切面,在切面中 ...

  4. Spring中两种编程式事务管理

    Spring中两种编程式事务管理 在代码中显示调用beginTransaction,commit,rollback等与事务处理相关的方法,这就是编程式事务管理,当只有少数事务操作时,编程式事务管理才比 ...

  5. Spring AOP两种实现机制是什么?

    Spring AOP两种实现机制是什么? 1.如果是有接口声明的类进行AOP 时,spring调用的是java.lang.reflection.Proxy 类来做处理 2.如果是没有接口声明的类时, ...

  6. Spring的两种任务调度Scheduled和Async

    Spring提供了两种后台任务的方法,分别是: 调度任务,@Schedule 异步任务,@Async 当然,使用这两个是有条件的,需要在spring应用的上下文中声明 <task:annotat ...

  7. Spring AOP两种使用方式以及如何使用解析

    AOP是一种面向切面编程思想,也是面向对象设计(OOP)的一种延伸. 在Spring实现AOP有两种实现方式,一种是采用JDK动态代理实现,另外一种就是采用CGLIB代理实现,Spring是如何实现的 ...

  8. Spring的两种代理方式:JDK动态代理和CGLIB动态代理

    代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为"代理",所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者 ...

  9. Spring的两种动态代理:Jdk和Cglib 的区别和实现

    这是有意义的一天!自己研究一路畅通的感觉真爽 原理是参考大神的,代码手敲 一.原理区别: java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处 ...

最新文章

  1. myeclipse使用maven整合ssh配置
  2. Citrix策略处理与优先级
  3. vivox21升级鸿蒙,vivo X21i相机规格再升级,加持AI成又一拍照神器
  4. vs中没有fstream_vs++2010 编译说找不到 fstream.h 解决方法
  5. 关于input单选框的radio属性
  6. Python 开发桌面小工具,让代码替我们干重复的工作!
  7. spring-boot actuator(监控)配置和使用
  8. 谈论 NOD32: 教育网超级 病毒更新服务器: http://222.197.166.33/main.htm
  9. 深入理解二阶段提交协议(DDB对XA悬挂事务的处理分析)(一)
  10. 720-C语言实现2048游戏
  11. 关于Django响应速度慢的问题
  12. 合金电阻分类与合金电阻详细参数介绍
  13. 很多网友问那个磁力搜索站好用,就由本君说说吧!
  14. 计算机演示文稿实验报告,演示文稿实验报告
  15. adb命令重置_Android ADB 常用命令
  16. 纯干货分享,2021年阿里巴巴社招面试题总结,本人上周已成功入职!
  17. java 锟斤 解决乱码_java eclipse 开发中文乱码锟斤拷小锟斤拷锟
  18. ECShop后台详解-模块管理、基本信息设置、商品展示、促销管理、订单管理、文章管理、报表统计、数据备份与还原
  19. 【T+】畅捷通T+软件,修改固定资产模块中已经使用卡片的资产编码。
  20. xampp php搭建失败,ThinkPHP框架搭建及常见问题(XAMPP安装失败、Apache/MySQL启动失败),thinkphpxampp...

热门文章

  1. 203. 移除链表元素(C语言)
  2. [armv9]-ARMV9 CCA 机密计算简介
  3. MySQL 开发日志 -- 性能调优
  4. 2017报计算机热不热,2017年五月份热吗?2017年五月天气热不热?
  5. 埃拉托斯特尼筛法(埃筛)
  6. linux route命令删除多余路由
  7. 【安全研究】从mimikatz学习万能密码——上
  8. 世安杯CTF writeup
  9. 配置SQL Server数据库连接
  10. 1.10 字符串的替换(replace()、replaceFirst()和replaceAll())