Spring BeanPostProcessor执行时机和Bean的初始化时机总结

来源:互联网 发布:网络教育学生档案 编辑:程序博客网 时间:2024/06/06 09:22
Spring BeanPostProcessor:针对所有Spring上下文中所有的bean
可以在配置文档applicationContext.xml中配置一个BeanPostProcessor,然后对所有的bean进行一个初始化之前和之后的代理
Bean初始化执行的顺序:先执行BeanPostProcessor实现InitializingBean 后执行afterPropertiesSet方法
然后执行init-method方法
测试用例:
xml文件配置:
<bean id="testBean" class="com.processor.TestBean" init-method="init" lazy-init="false"/><bean id="testProcessor" class="com.processor.TestBeanProcessor"/><bean id="MyBeanFactory" class="com.processor.MyBeanFactory"/><aop:aspectj-autoproxy/><bean id="testAsBean" class="com.aspectJ.ExampleBean"/><bean id="testAdvice" class="com.aspectJ.TestAdvice"/><bean id="testAdvice2" class="com.aspectJ.TestAdvice2"/>

测试的Bean实例
public class TestBean implements InitializingBean {    String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public void init() {        System.out.println("intMethod is called");    }    @PostConstruct //j2EE的注解。现在不生效    public void initPost() {        System.out.println("PostConstruct is called");    }    @Override    public void afterPropertiesSet() throws Exception {        System.out.println("afterPropertiesSet is called");    }}
BeanPostProcessor的编写
public class TestBeanProcessor implements BeanPostProcessor {    @Override    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {        System.out.println("postProcessBeforeInitialization" + beanName + " is called");        if (bean.getClass().isAnnotationPresent(TypeAnnotation.class)) {            return bean;        }        return bean;    }    @Override    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {        System.out.println("postProcessAfterInitialization" + beanName + " is called");        return bean;    }}

Junit测试用例
@Testpublic void testDubbo() {    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");    People p = (People) ctx.getBean("cutesource");    System.out.println(p.getId());    System.out.println(p.getName());    System.out.println(p.getAge());}

执行test可以看到打印的顺序
postProcessBeforeInitialization cutesource is called
postProcessAfterInitialization cutesource is called
postProcessBeforeInitialization testBean is called
afterPropertiesSet is called
intMethod is called
postProcessAfterInitialization testBean is called
postProcessBeforeInitialization MyBeanFactory is called
postProcessAfterInitialization MyBeanFactory is called
postProcessBeforeInitialization testAsBean is called
postProcessAfterInitialization testAsBean is called
postProcessBeforeInitialization testAdvice is called
postProcessAfterInitialization testAdvice is called
postProcessBeforeInitialization testAdvice2 is called
postProcessAfterInitialization testAdvice2 is called
cutesource
aaa
27

对每一个单例Bean的初始化都会 执行 BeanPostProcessor 的两个方法。调用时机通过查看源码
在:
AbstractAutowireCapableBeanFactory 中的 initializeBean()中,这个方法里会调用
applyBeanPostProcessorsBeforeInitialization()和applyBeanPostProcessorsAfterInitialization()方法
同时有invokeInitMethods()方法。这个方法里会调用afterPropertiesSet()
和invoke反射执行init-method方法.