Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)

一、容器后处理器

  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.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                           http://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{   
    @Override
    public 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类时的id
        user.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=agx987654


com.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值