bean装载到Spring应用上下文的生命周期
在基于Spring的应用中,应用对象生存于Spring容器中,Spring容器负责创建对象,装配对象,配置对象,并管理对象的整个生命周期,从生存到死亡。
bean装载到Spring应用上下文中的生命周期过程如图所示:
过程解释:
1. Spring对bean进行实例化;
2. Spring将值和bean的引用注入到bean对应的属性中;
3. 如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName()方法;
4. 如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入;
5. 如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext()方法,将bean所在的应用上下文的引用传入进来;
6. 如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeIniitialization()方法;
7. 如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()方法。
8. 如果bean使用initMethod声明了初始化方法,该方法也会被调用;
9. 如果bean实现了BeanPostProcessor接口,Spring将调用的postProcessAfterInitialization()方法;
10. 此时,bean已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该应用上下文被销毁;
11. 如果bean实现了DisposableBean接口,Spring将调用它的destroy()接口方法。
12. 如果bean使用destroy-method声明了销毁方法,该方法也会被调用。
测试代码:
package beanlifecycle; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * bean的定义 */ public class MySpringBeanLifeCycle implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean { private ApplicationContext applicationContext; public MySpringBeanLifeCycle() { System.out.println("实例化 MySpringBeanLifeCycle ..."); } @Override public void setBeanName(String s) { System.out.println("BeanNameAware -> setBeanName ..."); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("BeanFactoryAware --> setBeanFactory ..."); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("ApplicationContextAware -> setApplicationContext ..."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean -> afterPropertiesSet ..."); } @Override public void destroy() throws Exception { System.out.println("DisposableBean -> destroy..."); } public void customInit() { System.out.println("自定义初始化方法..."); } public void customDestroy() { System.out.println("自定义销毁方法..."); } }
package beanlifecycle; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * 实现BeanPostProcessor接口 */ public class MySpringBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof MySpringBeanLifeCycle) { System.out.println("BeanPostProcessor -> postProcessBeforeInitialization ..."); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof MySpringBeanLifeCycle) { System.out.println("BeanPostProcessor -> postProcessAfterInitialization ..."); } return bean; } }
package beanlifecycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * bean配置类 */ @Configuration public class BeanLifeCycleConfig { @Bean(initMethod = "customInit", destroyMethod = "customDestroy") public MySpringBeanLifeCycle mySpringBeanLifeCycle() { return new MySpringBeanLifeCycle(); } @Bean public MySpringBeanPostProcessor mySpringBeanPostProcessor() { return new MySpringBeanPostProcessor(); } }
package beanlifecycle; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanLifeCycleConfig.class); context.getBean(MySpringBeanLifeCycle.class); context.destroy(); } }
运行结果
实例化 MySpringBeanLifeCycle ... BeanNameAware -> setBeanName ... BeanFactoryAware --> setBeanFactory ... ApplicationContextAware -> setApplicationContext ... BeanPostProcessor -> postProcessBeforeInitialization ... InitializingBean -> afterPropertiesSet ... 自定义初始化方法... BeanPostProcessor -> postProcessAfterInitialization ... DisposableBean -> destroy... 自定义销毁方法...
参考《Spring实战(第4版)》
相关文章