spring使用BeanFactory来实例化、配置和管理对象,可是它只是一个接口,里面有一个getBean()方法。咱们通常都不直接用BeanFactory,而是用它的实现类ApplicationContext,这个类会自动解析咱们配置的applicationContext.xml,而后根据咱们配置的bean来new对象,将new好的对象放进一个Map中,键就是咱们bean的id,值就是new的对象。java
经常使用的设定方式有如下三种:spring
public class InitSequenceBean implements InitializingBean { public InitSequenceBean() { System.out.println("InitSequenceBean: constructor"); } @PostConstruct public void postConstruct() { System.out.println("InitSequenceBean: postConstruct"); } public void initMethod() { System.out.println("InitSequenceBean: init-method"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitSequenceBean: afterPropertiesSet"); } }
而且在配置文件中添加以下Bean定义:app
<bean class="InitSequenceBean" init-method="initMethod"></bean>
输出结果:ide
InitSequenceBean: constructor InitSequenceBean: postConstruct InitSequenceBean: afterPropertiesSet InitSequenceBean: init-method
经过上述输出结果,说明三种初始化的顺序是:
Constructor > @PostConstruct > InitializingBean > init-methodpost
缘由:
@PostConstruct注解后的方法在BeanPostProcessor前置处理器中就被执行了。咱们知道BeanPostProcessor接口是一个回调的做用,spring容器的每一个受管Bean在调用初始化方法以前,都会得到BeanPostProcessor接口实现类的一个回调。在BeanPostProcessor的方法中有一段逻辑就是会判断当前被回调的bean的方法中有没有被initAnnotationType/destroyAnnotationType注释,若是有,则添加到init/destroy队列中,后续一一执行。initAnnotationType/destroyAnnotationType注解就是@PostConstruct/@PreDestroy。因此@PostConstruct固然要先于InitializingBean和init-method执行了。prototype
从图中,咱们能够看到实例化Bean的过程当中有如下几个节点:code