bean后处理器和容器后处理器
1.bean后处理器
这种处理器会对容器中的bean进行后处理,对bean的功能进行额外加强
bean的后处理器必须实现接口BeanPostProcessor,该接口包含两个方法
public Object postProcessBeforeInitialization(Object
bean, String beanName)
public Object postProcessAfterInitialization(Object
bean, String beanName)
下面定义一个bean后处理器
import org.springframework.beans.BeansException;
import
org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements
BeanPostProcessor {
public Object
postProcessBeforeInitialization(Object bean,
String beanName)
throws BeansException {
System.out.println(bean);
System.out.println("bean后处理器在bean初始化之前对"+beanName+"进行增强处理!");
return
bean; }
public Object
postProcessAfterInitialization(Object bean, String
beanName)
throws
BeansException {
System.out.println("bean后处理器在bean初始化之后对"+beanName+"进行增强处理!");
if(bean instanceof
American){
American a =
(American)bean;
a.setName("后处理器重新设置的值");
} return
bean;
}
下面是测试使用的bean类
斧头的接口
public interface Axe {
String chop();
}
普通人的接口
public interface Person {
void useAxe();
}
斧头的实现类
import com.spring.being.Axe;
public class SteelAxe implements Axe {
private int count;
public SteelAxe(){
System.out.println("SteelAxe的无参构造器被调用......");
}
public String chop() {
return
"铁斧用了"+(++count)+"次";
}
}
普通人的实现类(实现接口Person和InitializingBean )
import org.springframework.beans.factory.InitializingBean;
import com.spring.being.Axe;
import com.spring.being.Person;
public class American
implements Person,InitializingBean {
private Axe axe;
private String name;
public American(){
System.out.println("Chinese的无参构造器被调用.......");
}
public American(Axe axe){
this.axe=axe;
System.out.println("Chinese的有参构造器被调用.......");
}
//public abstract Axe
createAxe();//设置一个抽象类,该方法由spring负责实现
public void setAxe(Axe axe) {
System.out.println("Spring执行依赖关系注入.......");
this.axe = axe;
}
public Axe getAxe() {
return axe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void useAxe() {
System.out.println("美国人使用:"+axe.chop());
System.out.println("参数name:"+name);
}
public void
init(){
//查看bean后处理器是在bean初始化之前还是之后起作用的呢,书写该方法该类必须在配置市加上属性init-method="init",方法名是任意的,只要一致就行
System.out.println("正在执行初始化方法init......");
}
public void afterPropertiesSet()
throws Exception {
//查看bean后处理器是在bean初始化之前还是之后起作用的呢,书写该方法该类必须实现接口InitializingBean
System.out.println("正在执行初始化方法afterPropertiesSet......");
}
}
书写配置文件
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www./schema/beans"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://www./schema/beans
http://www./schema/beans/spring-beans-2.5.xsd">
<bean id="axe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="steelAxe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="American"
class="com.spring.postprocessor.American"
init-method="init">
<property
name="axe" ref="steelAxe" />
<property
name="name" value="依赖注入的值"/>
</bean>
<!--配置bean后处理器 -->
<bean
id="beanPostProcessor"
class="com.spring.postprocessor.MyBeanPostProcessor"
/> </beans>
书写一个测试类
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.being.Person;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new
ClassPathXmlApplicationContext("applicationContext-deppen.xml");//用该方法加载可以自动注册bean后处理器,否则需要手动注册
Person p =
(Person)factory.getBean("American");
p.useAxe();
}
}
测试结果:
bean后处理器在bean初始化之前对American进行增强处理! 正在执行初始化方法afterPropertiesSet......
正在执行初始化方法init......
bean后处理器在bean初始化之后对American进行增强处理!
美国人使用:铁斧用了1次
参数name:后处理器重新设置的值 //bean的参数值已被后处理器修改了
spring提供的两种常用的后处理器
BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator:根据提供的Advisor,对容器中的所有bean创建代理
2.容器后处理器
容器必须实现接口BeanFactoryPostProcessor,该接口有方法:
public void
postProcessBeanFactory(ConfigurableListableBeanFactory
beanFactory) throws
BeansException
书写一个容器后处理器:
import org.springframework.beans.BeansException;
import
org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements
BeanFactoryPostProcessor {
public void
postProcessBeanFactory(ConfigurableListableBeanFactory
beanFactory) throws
BeansException {
System.out.println("本程序对spring所做的beanfactory的初始化没有意见。。。。");
System.out.println("spring容器是:"+beanFactory);
System.out.println("可以取得bean:"+beanFactory.getBean("American"));
}
}
配置文件
在上面的配置文件再加上
<!--配置容器后处理器 -->
<bean id="beanFactoryPostProcessor"
class="com.spring.postprocessor.MyBeanFactoryPostProcessor"
/>
测试类
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.being.Person;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new
ClassPathXmlApplicationContext("applicationContext-deppen.xml");
Person p =
(Person)factory.getBean("American");
p.useAxe();
}
}
输出结果:
本程序对spring所做的beanfactory的初始化没有意见。。。。
spring容器是:org.springframework.beans.factory.suppor
spring提供的几种常见的容器后处理器
1.PropertyPlaceholderConfigurer属性占位符配置器
2.PropertyOverrideConfigurer重写占位符配置器
对PropertyPlaceholderConfigurer的测试如下:
配置文件applicationContext-deppen.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www./schema/beans"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://www./schema/beans
http://www./schema/beans/spring-beans-2.5.xsd">
<bean id="axe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="steelAxe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="American"
class="com.spring.postprocessor.American"
init-method="init">
<property
name="axe" ref="steelAxe" />
<property name="name"
value="${person.name}"
/> </bean>
<!-- 配置属性占位符配置器 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>person.properties</value>
</list>
</property>
</bean>
</beans>
配置文件person.properties:
person.name=\u4F9D\u8D56\u6CE8\u5165\u7684\u503C
测试类:
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.being.Person;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new
ClassPathXmlApplicationContext("applicationContext-deppen.xml");
Person p =
(Person)factory.getBean("American");
p.useAxe();
}
}
输出结果:
Chinese的无参构造器被调用.......
SteelAxe的无参构造器被调用......
Spring执行依赖关系注入.......
正在执行初始化方法afterPropertiesSet......
正在执行初始化方法init......
美国人使用:铁斧用了1次
参数name:依赖注入的值
|