package com.xx;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import com.xx.service.DemoService;
@Component
public class InitBeanTest implements InitializingBean,ApplicationListener<ContextRefreshedEvent> {
@Resource
DemoService demoService;
public InitBeanTest() {
System.err.println("----> InitSequenceBean: constructor: "+demoService);
}
@PostConstruct
public void postConstruct() {
System.err.println("----> InitSequenceBean: postConstruct: "+demoService);
}
@Override
public void afterPropertiesSet() throws Exception {
System.err.println("----> InitSequenceBean: afterPropertiesSet: "+demoService);
}
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
System.err.println("----> InitSequenceBean: onApplicationEvent");
}
}
执行结果
----> InitSequenceBean: constructor: null
----> InitSequenceBean: postConstruct: com.yiniu.kdp.service.impl.DemoServiceImpl@40fe544
----> InitSequenceBean: afterPropertiesSet: com.yiniu.kdp.service.impl.DemoServiceImpl@40fe544
----> InitSequenceBean: onApplicationEvent
----> InitSequenceBean: onApplicationEvent
分析
构造函数是每个类最先执行的,这个时候,bean属性还没有被注入
postConstruct优先于afterPropertiesSet执行,这时属性竟然也被注入了,有点意外
spring很多组建的初始化都放在afterPropertiesSet做。我们在做一些中间件想和spring一起启动,可以放在这里启动。
onApplicationEvent属于应用层的时间,最后被执行,很容易理解。注意,它出现了两次,为什么?因为bean注入了DemoService,spring容器会被刷新。
换言之onApplicationEvent会被频繁执行,需要使用它监听,需要考虑性能问题。
很显然,这是观察者模式的经典应用。
总结
1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过init-method指定,两种方式可以同时使用
2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是init-method方式消除了对spring的依赖
3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。
源码分析参考:https://www.cnblogs.com/april-chen/p/8182631.html
文章参考:https://blog.csdn.net/zhairuiping1989/article/details/78859931