|
| 1 | +## Spring Bean 生命周期 |
| 2 | + |
| 3 | + |
| 4 | +### 前言 |
| 5 | + |
| 6 | +Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。 |
| 7 | + |
| 8 | +首先放一张很经典的生命周期图: |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +再谈生命周期之前有一点需要先明确: |
| 13 | + |
| 14 | +> Spring 只帮我们管理单例模式 Bean 的**完整**生命周期,对于 prototype 的 bean ,Spring 在创建好交给使用者之后则不会再管理后续的生命周期。 |
| 15 | +
|
| 16 | + |
| 17 | +### 注解方式 |
| 18 | + |
| 19 | +在 bean 初始化时会经历几个阶段,首先可以使用注解 `@PostConstruct`, `@PreDestroy` 来再 bean 的创建和销毁阶段进行调用: |
| 20 | + |
| 21 | +```java |
| 22 | +@Component |
| 23 | +public class AnnotationBean { |
| 24 | + private final static Logger LOGGER = LoggerFactory.getLogger(AnnotationBean.class); |
| 25 | + |
| 26 | + @PostConstruct |
| 27 | + public void start(){ |
| 28 | + LOGGER.info("AnnotationBean start"); |
| 29 | + } |
| 30 | + |
| 31 | + @PreDestroy |
| 32 | + public void destroy(){ |
| 33 | + LOGGER.info("AnnotationBean destroy"); |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### InitializingBean, DisposableBean 接口 |
| 39 | + |
| 40 | +还可以实现 `InitializingBean,DisposableBean` 这两个接口,也是在初始化已经销毁阶段调用: |
| 41 | + |
| 42 | +```java |
| 43 | +@Service |
| 44 | +public class SpringLifeCycleService implements InitializingBean,DisposableBean{ |
| 45 | + private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleService.class); |
| 46 | + @Override |
| 47 | + public void afterPropertiesSet() throws Exception { |
| 48 | + LOGGER.info("SpringLifeCycleService start"); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void destroy() throws Exception { |
| 53 | + LOGGER.info("SpringLifeCycleService destroy"); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### 自定义初始化和销毁方法 |
| 59 | + |
| 60 | +也可以自定义方法用于在初始化、销毁阶段进行调用: |
| 61 | + |
| 62 | +```java |
| 63 | +@Configuration |
| 64 | +public class LifeCycleConfig { |
| 65 | + |
| 66 | + |
| 67 | + @Bean(initMethod = "start", destroyMethod = "destroy") |
| 68 | + public SpringLifeCycle create(){ |
| 69 | + SpringLifeCycle springLifeCycle = new SpringLifeCycle() ; |
| 70 | + |
| 71 | + return springLifeCycle ; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public class SpringLifeCycle{ |
| 76 | + |
| 77 | + private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycle.class); |
| 78 | + public void start(){ |
| 79 | + LOGGER.info("SpringLifeCycle start"); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public void destroy(){ |
| 84 | + LOGGER.info("SpringLifeCycle destroy"); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +以上是在 SpringBoot 中可以这样配置,如果是原始的基于 XML 也是可以利用: |
| 90 | + |
| 91 | +``` |
| 92 | +<bean class="com.crossoverjie.spring.SpringLifeCycle" init-method="start" destroy-method="destroy"> |
| 93 | +</bean> |
| 94 | +``` |
| 95 | + |
| 96 | +来达到同样的效果。 |
| 97 | + |
| 98 | +### 实现 *Aware 接口 |
| 99 | + |
| 100 | +`*Aware` 接口可以用于在初始化 bean 时获得 Spring 中的一些对象,如获取 `Spring 上下文`等。 |
| 101 | + |
| 102 | +```java |
| 103 | +@Component |
| 104 | +public class SpringLifeCycleAware implements ApplicationContextAware { |
| 105 | + private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleAware.class); |
| 106 | + |
| 107 | + private ApplicationContext applicationContext ; |
| 108 | + |
| 109 | + @Override |
| 110 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 111 | + this.applicationContext = applicationContext ; |
| 112 | + LOGGER.info("SpringLifeCycleAware start"); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +这样在 `springLifeCycleAware` 这个 bean 初始化会就会调用 `setApplicationContext` 方法,并可以获得 applicationContext 对象。 |
| 118 | + |
| 119 | +### BeanPostProcessor 后处理器 |
| 120 | + |
| 121 | +也可以实现 BeanPostProcessor 接口,Spring 中所有 bean 在做初始化时都会调用该接口中的两个方法,可以用于对一些特殊的 bean 进行处理: |
| 122 | + |
| 123 | +```java |
| 124 | +@Component |
| 125 | +public class SpringLifeCycleProcessor implements BeanPostProcessor { |
| 126 | + private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleProcessor.class); |
| 127 | + @Override |
| 128 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 129 | + return bean; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 134 | + return bean; |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +执行之后观察结果: |
| 140 | + |
| 141 | +``` |
| 142 | +018-03-21 00:40:24.856 [restartedMain] INFO c.c.s.p.SpringLifeCycleProcessor - SpringLifeCycleProcessor start beanName=annotationBean |
| 143 | +2018-03-21 00:40:24.860 [restartedMain] INFO c.c.spring.annotation.AnnotationBean - AnnotationBean start |
| 144 | +2018-03-21 00:40:24.861 [restartedMain] INFO c.c.s.p.SpringLifeCycleProcessor - SpringLifeCycleProcessor end beanName=annotationBean |
| 145 | +2018-03-21 00:40:24.864 [restartedMain] INFO c.c.s.aware.SpringLifeCycleAware - SpringLifeCycleAware start |
| 146 | +2018-03-21 00:40:24.867 [restartedMain] INFO c.c.s.service.SpringLifeCycleService - SpringLifeCycleService start |
| 147 | +2018-03-21 00:40:24.887 [restartedMain] INFO c.c.spring.SpringLifeCycle - SpringLifeCycle start |
| 148 | +2018-03-21 00:40:25.062 [restartedMain] INFO o.s.b.d.a.OptionalLiveReloadServer - LiveReload server is running on port 35729 |
| 149 | +2018-03-21 00:40:25.122 [restartedMain] INFO o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup |
| 150 | +2018-03-21 00:40:25.140 [restartedMain] INFO com.crossoverjie.Application - Started Application in 2.309 seconds (JVM running for 3.681) |
| 151 | +2018-03-21 00:40:25.143 [restartedMain] INFO com.crossoverjie.Application - start ok! |
| 152 | +2018-03-21 00:40:25.153 [Thread-8] INFO o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3913adad: startup date [Wed Mar 21 00:40:23 CST 2018]; root of context hierarchy |
| 153 | +2018-03-21 00:40:25.155 [Thread-8] INFO o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown |
| 154 | +2018-03-21 00:40:25.156 [Thread-8] INFO c.c.spring.SpringLifeCycle - SpringLifeCycle destroy |
| 155 | +2018-03-21 00:40:25.156 [Thread-8] INFO c.c.s.service.SpringLifeCycleService - SpringLifeCycleService destroy |
| 156 | +2018-03-21 00:40:25.156 [Thread-8] INFO c.c.spring.annotation.AnnotationBean - AnnotationBean destroy |
| 157 | +``` |
| 158 | + |
| 159 | + |
| 160 | + |
0 commit comments