一、定义三个BeanPostProcessor
package cn.edu.pku;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyScannedBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("my scanned BeanPostProcessor......before: " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("my scanned BeanPostProcessor......after " + beanName);
return bean; }
}
package cn.edu.pku;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyXmlBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println("my xml BeanPostProcessor......before : " + beanName);
return bean; //
}
public Object postProcessAfterInitialization(Object bean, String beanName) {
System.out.println("my xml BeanPostProcessor......after : " + beanName);
return bean;
}
}
package cn.edu.pku;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyManualBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("my manual BeanPostProcessor......before: " + beanName);
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("my manual BeanPostProcessor......after : " + beanName);
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
二、定义bean类:
package cn.edu.bju.service;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Locale;
@Service
public class PrintService {
public String printInfo(String info){
System.out.println(info);
return info.toUpperCase();
}
}
三、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="printService" class="cn.edu.bju.service.PrintService" lazy-init="true"/>
<context:component-scan base-package="cn.edu.pku"/>
<bean class="cn.edu.pku.MyXmlBeanPostProcessor"/>
</beans>
四、定义主类
package cn.edu.tju.test;
import cn.edu.pku.MyManualBeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Test04{
public static void main(final String[] args) throws Exception {
ConfigurableApplicationContext ctx = new
ClassPathXmlApplicationContext("spring3.xml");
ctx.getBeanFactory().addBeanPostProcessor(new MyManualBeanPostProcessor());
Object printService = ctx.getBean("printService");
System.out.println(printService);
}
}
五、运行结果:
可以看到,ComponentScan扫描到的BeanPostProcessor最先执行,xml配置文件里的随后执行,而我们手动添加的BeanPostProcessor最后执行(因为它在保存BeanPostProcessor的CopyOnWriteArrayList的末尾)###############################
执行顺序貌似和官方文档描述有不同