spring bean初始化顺序(不包括BeanFactory的创建/属性注入准备)
Constructor(构造函数) > @PostConstruct > InitializingBean > init-method
代码验证如下。主要注意的是@PostConstruct执行的InitDestroyAnnotationBeanPostProcessor的postProcessBeforeInitialization
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
public class InitSequenceBean implements InitializingBean, DisposableBean {
public InitSequenceBean() { System.out.println("InitSequenceBean: constructor"); }
@PostConstruct public void postConstruct() { System.out.println("InitSequenceBean: postConstruct"); }
@PreDestroy public void preDestory() { System.out.println("InitSequenceBean: preDestroy"); }
public void initMethod() { System.out.println("InitSequenceBean: init-method"); }
public void destroyMethod() { System.out.println("InitSequenceBean: destroy-method"); }
@Override public void afterPropertiesSet() throws Exception { System.out.println("InitSequenceBean: afterPropertiesSet"); }
@Override public void destroy() throws Exception { System.out.println("InitSequenceBean: destroy"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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">
<bean class="com.stone.studio.springdemo.lifecycle.InitSequenceBean" init-method="initMethod" destroy-method="destroyMethod"/>
</beans>
|
参考