Spring Bean的一生
When you work directly in Java, you can do anything you like with your objects and do not always need to rely on the container lifecycle.
前言:
在Ioc容器启动后相应的Bean并没有立即实例化,此时Ioc容器仅仅拥有所有对象的BeanDefinition(Bean对象在Spring中的描述,包含该Bean在容器中实例化所需的信息,通过解析xml/javaconfig后所得)
Spring Bean生命周期的管理 Spring bean factory
能管理Beans
的生命周期,通过实现InitializingBean 和DisposableBean
这两个接口都只声明一个方法让我们可以初始化/关闭
bean中的资源
当然在整个生命周期中也提供了一些Call Back Methods ,这些方法类似于Servlet
中的监听器的实现
当Bean
处于post-initialization
阶段时,我们可以通过实现InitializingBean
接口来实现afterPropertiesSet()
当Bean
处于pre-destruction
阶段时,我们可以实现DisposableBean
接口来实现destroy()
方法
Spring Bean的生命周期图
Code Time 以Rick
类交给Spring容器管理,方法顺序为其在Spring容器中的执行顺序
Rick.java
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 public class Rick implements InitializingBean , DisposableBean { public Rick () { System.out.println("Rick is in [Rick] - Rick()" ); } public void customInit () { System.out.println("Rick is in [Rick] - customInit()" ); } @PostConstruct public void postConstruct () { System.out.println("Rick is in [@PostConstruct] - postConstruct()" ); } @Override public void afterPropertiesSet () throws Exception { System.out.println("Rick is in [InitializingBean] - afterPropertiesSet()" ); } @PreDestroy public void preDestroy () { System.out.println("Rick is in [@PreDestroy] - preDestroy()" ); } @Override public void destroy () throws Exception { System.out.println("Rick is in [DisposableBean] - destroy()" ); } public void customDestroy () { System.out.println("Rick is in [Rick] - customDestroy()" ); } }
加入BeanPostProcess
RickBeanPostProcess.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class RickBeanPostProcess implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { if (bean instanceof Rick) { System.out.println("Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()" ); } return bean; } @Override public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException { if (bean instanceof Rick) { System.out.println("Rick is in [BeanPostProcessor] - postProcessAfterInitialization()" ); } return bean; } }
配置类
RickConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Configuration public class RickConfig { @Bean(initMethod = "customInit",destroyMethod = "customDestroy") public Rick rick () { return new Rick(); } @Bean public RickBeanPostProcess rickBeanPostProcess () { return new RickBeanPostProcess(); } }
测试类
Client.java
1 2 3 4 5 6 public class Client { public static void main (String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RickConfig.class); ((AnnotationConfigApplicationContext) applicationContext).close(); } }
最终结果如下
1 2 3 4 5 6 7 8 9 Rick is in [Rick] - Rick() Rick is in [BeanPostProcessor] - postProcessBeforeInitialization() Rick is in [@PostConstruct] - postConstruct() Rick is in [InitializingBean] - afterPropertiesSet() Rick is in [Rick] - customInit() Rick is in [BeanPostProcessor] - postProcessAfterInitializa Rick is in [@PreDestroy] - preDestroy() Rick is in [DisposableBean] - destroy() Rick is in [Rick] - customDestroy()
参考 Spring Framework Documentation
Spring Bean Life Cycle Explained
Spring Bean Life Cycle
Spring Boot Bean生命周期
Spring的Bean生命周期理解