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

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测试用例
@Test
public 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方法.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值