Bean后处理器是一种特殊的Bean,这种特殊的Bean并不对外提供服务,它主要负责对容器中的其他Bean执行后处理。
Bean后处理器必须实现BeanPostProcessor接口,BeanPostProcessor包含如下两个方法:
☞ public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
该方法第一个参数,是系统即将惊醒后处理的Bean实例,第二个参数是该Bean实例的名字
☞ public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
该方法第一个参数,是系统即将惊醒后处理的Bean实例,第二个参数是该Bean实例的名字
实现该接口的Bean后处理器必须实现这两个方法,这两个方法会对容器的Bean进行后处理,会在目标Bean初始化之前、初始化之后分别被回调,这两个方法用于对容器中Bean实例进行增强处理。
下面是spring提供的两个常用的后处理器:
☞ BeanNameAutoProxyCreator:根据Bean实例的name属性,创建bean实例的代理
☞ DefaultAdvisorAutoProxyCreator:根据提供的Advisor,对容器中所有的Bean实例创建代理
容器后处理器负责处理容器本身,容器后处理器必须实现BeanFactoryPostProcessor接口。实现该接口必须实现如下方法:
☞ postProcessorBeanFactory(ConfigurableListableBeanFactory beanFactory)
Spring 已经提供了如下几个常用的容器后处理器:
☞ PropertyPlaceholderConfigurer:属性占位符配置器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的元数据
☞ PropertyOverrideConfigurer:重写占位符配置器
☞ CustomAutowireConfigurer:自定义自动装配的配置器
☞ CustomScopeConfigurer:自定义作用域的配置器
bean.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>dbconn.propertiesvalue>
list>
property>
bean>
<bean id="dataSource" class="main.java.dao.DataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
bean>
beans>
dbconn.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/javaee
jdbc.username=root
jdbc.password=qx.29682
PropertyOverrideConfigurer的属性文件指定的信息可以直接覆盖Spring配置文件中的元数据,比PropertyPlaceholderConfigurer要强大。
使用PropertyOverrideConfigurer的属性文件,每条属性应保持如下的格式:
beanName.property=value
beanName是属性占位符试图覆盖的Bean名,property是试图覆盖的属性名。
bean.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<bean
class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>dbconn.propertiesvalue>
list>
property>
bean>
<bean id="dataSource" class="main.java.dao.DataSource" />
beans>
dbconn.properties:
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/javaee
dataSource.username=root
dataSource.password=qx.29682
main.java.dao.DataSource.java:
main.java.dao.DataSource:
package main.java.dao;
public class DataSource {
private String driverClass;
private String url;
private String username;
private String password;
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void jdbcMessage() {
System.out.println("driverClass:" + driverClass);
System.out.println("url:" + url);
System.out.println("username:" + username);
System.out.println("password:" + password);
}
}