一、容器后处理器
Spring有如下几个常用容器后处理器:
PropertyPlaceholderConfigurer:属性点位符配置器
PropertyOverrideConfigurer:重写占位符配置器
CustomAutowireConfigurer:自定义自动装配的配置器
CustomScopeConfigurer:自定义作用域的配置器容器后处理器用于负责处理容器本身,须实现BeanFactoryPostProcessor接口
并实现该接口的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法。如果采用ApplicationContext作为Spring容器,会自动注册容器后处理器,如果采用BeanFactory作为Spring容器,则需要手动调用该容器来处理Spring容器。
代码示例:
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" />