|
| 1 | +# Spring bean 的生命周期 |
| 2 | + |
| 3 | +## 概述 |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +1. 注入属性 |
| 8 | +2. `Aware` 的 setter 方法注入资源 |
| 9 | +3. `BeanPostProcessor` 进行初始化前后的处理(`BeanPostProcessor`实现类不能处理 `BeanPostProcessor`实现类) |
| 10 | +4. `@PostConstruct` |
| 11 | +5. `@PreDestroy` |
| 12 | + |
| 13 | +## 测试类 |
| 14 | + |
| 15 | +在 [boot-init](https://github.com/c-rainstorm/boot-init) 项目中, `BeanLifeCycleAllInOne`,`AfterBeanInitial` |
| 16 | + |
| 17 | +```java |
| 18 | +@Slf4j |
| 19 | +@Component |
| 20 | +public class BeanLifeCycleAllInOne implements BeanNameAware, ApplicationContextAware, BeanFactoryAware, EnvironmentAware, ResourceLoaderAware { |
| 21 | + |
| 22 | + @Override |
| 23 | + public void setBeanName(String name) { |
| 24 | + log.info("BeanNameAware.setBeanName -- {}", name); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 29 | + log.info("ApplicationContextAware.setApplicationContext"); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + @Override |
| 34 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 35 | + log.info("BeanFactoryAware.setBeanFactory"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void setEnvironment(Environment environment) { |
| 40 | + log.info("EnvironmentAware.setEnvironment"); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setResourceLoader(ResourceLoader resourceLoader) { |
| 46 | + log.info("ResourceLoaderAware.setResourceLoader"); |
| 47 | + } |
| 48 | + |
| 49 | + @PostConstruct |
| 50 | + public void postConstruct() { |
| 51 | + log.info("BeanLifeCycleAllInOne.postConstruct"); |
| 52 | + } |
| 53 | + |
| 54 | + @PreDestroy |
| 55 | + public void preDestroy() { |
| 56 | + log.info("BeanLifeCycleAllInOne.preDestroy"); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +```java |
| 62 | +@Slf4j |
| 63 | +@Component |
| 64 | +public class AfterBeanInitial implements BeanPostProcessor { |
| 65 | + @Override |
| 66 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 67 | + if (BeanLifeCycleAllInOne.class.getSimpleName().equalsIgnoreCase(beanName)) { |
| 68 | + log.info("AfterBeanInitial.postProcessBeforeInitialization -- {}", beanName); |
| 69 | + } |
| 70 | + return bean; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 75 | + if (BeanLifeCycleAllInOne.class.getSimpleName().equalsIgnoreCase(beanName)) { |
| 76 | + log.info("AfterBeanInitial.postProcessAfterInitialization -- {}", beanName); |
| 77 | + } |
| 78 | + return bean; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +## 测试输出 |
| 85 | + |
| 86 | +```log |
| 87 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.l.bean.BeanLifeCycleAllInOne : BeanNameAware.setBeanName -- beanLifeCycleAllInOne |
| 88 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.l.bean.BeanLifeCycleAllInOne : BeanFactoryAware.setBeanFactory |
| 89 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.l.bean.BeanLifeCycleAllInOne : EnvironmentAware.setEnvironment |
| 90 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.l.bean.BeanLifeCycleAllInOne : ResourceLoaderAware.setResourceLoader |
| 91 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.l.bean.BeanLifeCycleAllInOne : ApplicationContextAware.setApplicationContext |
| 92 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.lifecycle.bean.AfterBeanInitial : AfterBeanInitial.postProcessBeforeInitialization -- beanLifeCycleAllInOne |
| 93 | +2019-09-01 11:05:36.426 INFO 53094 --- [ main] m.r.b.l.bean.BeanLifeCycleAllInOne : BeanLifeCycleAllInOne.postConstruct |
| 94 | +2019-09-01 11:05:36.427 INFO 53094 --- [ main] m.r.b.lifecycle.bean.AfterBeanInitial : AfterBeanInitial.postProcessAfterInitialization -- beanLifeCycleAllInOne |
| 95 | +
|
| 96 | +... |
| 97 | +
|
| 98 | +2019-09-01 11:06:57.845 INFO 53094 --- [ Thread-16] m.r.b.l.bean.BeanLifeCycleAllInOne : BeanLifeCycleAllInOne.preDestroy |
| 99 | +``` |
| 100 | + |
| 101 | +## 参考 |
| 102 | + |
| 103 | +1. [ Java 程序员必备的一些流程图 - 芋道源码](https://mp.weixin.qq.com/s/8D4OXsU7CoSHF68LtaF2YA) |
| 104 | +1. [Interface Aware](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/Aware.html) |
0 commit comments