目录java
参考连接1:Spring中Bean的生命周期是怎样的?git
参考连接2:Spring Bean 生命周期github
最近买了几本书,看了其中讲springboot
的一本书,看到了讲spring bean的生命周期这一节,书上只是提了个大概,感受不是本身想要的结果,仍是本身动手吧;虽然写得很差,可是记一记仍是对本身有好处的;spring
在网上找了几张图,感受有一张比较清楚:springboot
接下来讲一下几种方式吧,该项目使用springboot
构建的;如下代码均在改项目中完成app
新建一个bean
类,注意该bean类上并无使用注解ide
public class CustomBean { private final static Logger logger = LoggerFactory.getLogger(CustomBean.class); public void init(){ logger.info("CustomBean-init"); } public void destroy(){ logger.info("CustomBean-destroy"); } }
自定义bean
的初始化和销毁方法须要在配置文件中添加如下代码:函数
@Configuration public class LifeCycleConfig { /** * 自定义bean的initMethod和destroyMethod * @return */ @Bean(initMethod = "init",destroyMethod = "destroy") CustomBean customBean(){ return new CustomBean(); } }
使用@PostConstruct
和@PreDestroy
两个注解,其中 @PostConstruct
是在构方函数执行完以后指向性,@PreDestroy
是在bean
销毁前执行;post
该bean类上使用了Component
注解,固然,你还可使用另外的那三个注解中的某一个;code
@Component public class AnnotationBean { private static final Logger logger= LoggerFactory.getLogger(AnnotationBean.class); @PostConstruct public void init(){ logger.info("AnnotationBean-init"); } @PreDestroy public void destroy(){ logger.info("AnnotationBean-destroy"); } }
还能够继承InitializingBean
, DisposableBean
接口
@Component public class LifeCycleBean implements InitializingBean,DisposableBean { private static final Logger logger= LoggerFactory.getLogger(LifeCycleBean.class); @Override public void destroy() throws Exception { logger.info("LifeCycleBean-destroy"); } @Override public void afterPropertiesSet() throws Exception { logger.info("LifeCycleBean-afterPropertiesSet"); } }
在上边的图中可看见,有三个在最前面的三个Aware
接口类,这些接口的功能就不用去说了
@Component public class LifeCycleBeanAware implements BeanNameAware,BeanFactoryAware,ApplicationContextAware { private static final Logger logger= LoggerFactory.getLogger(LifeCycleBeanAware.class); @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { logger.info("bean工厂"); } @Override public void setBeanName(String s) { logger.info("bean的默认的名字:"+s); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { logger.info("bean上下文"); } }
这个类不是接口类,这个类中有两个方法postProcessBeforeInitialization
和postProcessAfterInitialization
@Component public class LifeCycleProcessor implements BeanPostProcessor { private static final Logger logger= LoggerFactory.getLogger(LifeCycleProcessor.class); private static final String DEFAULT_BEAN_NAME="annotationBean"; /** *在bean初始化以前执行 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if(DEFAULT_BEAN_NAME.equals(beanName)){ logger.info("bean初始化以前执行:"+beanName); } return bean; } /** *bean初始化以后执行 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(DEFAULT_BEAN_NAME.equals(beanName)){ logger.info("bean初始化以后执行:"+beanName); } return bean; } }
启动后的结果:
2018-09-08 16:25:55.238 INFO 2584 --- [ main] com.gyc.bean.LifeCycleProcessor : bean初始化以前执行:annotationBean 2018-09-08 16:25:55.238 INFO 2584 --- [ main] com.gyc.bean.AnnotationBean : AnnotationBean-init 2018-09-08 16:25:55.238 INFO 2584 --- [ main] com.gyc.bean.LifeCycleProcessor : bean初始化以后执行:annotationBean 2018-09-08 16:25:55.241 INFO 2584 --- [ main] com.gyc.bean.LifeCycleBean : LifeCycleBean-afterPropertiesSet 2018-09-08 16:25:55.246 INFO 2584 --- [ main] com.gyc.bean.LifeCycleBeanAware : bean的默认的名字:lifeCycleBeanAware 2018-09-08 16:25:55.246 INFO 2584 --- [ main] com.gyc.bean.LifeCycleBeanAware : bean工厂 2018-09-08 16:25:55.246 INFO 2584 --- [ main] com.gyc.bean.LifeCycleBeanAware : bean上下文 2018-09-08 16:25:55.279 INFO 2584 --- [ main] com.gyc.bean.CustomBean : CustomBean-init 2018-09-08 16:25:55.506 INFO 2584 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-09-08 16:25:55.539 INFO 2584 --- [ main] com.gyc.GycApplication : Started GycApplication in 1.922 seconds (JVM running for 3.01) 2018-09-08 16:25:55.544 INFO 2584 --- [ Thread-6] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7b227d8d: startup date [Sat Sep 08 16:25:54 CST 2018]; root of context hierarchy 2018-09-08 16:25:55.548 INFO 2584 --- [ Thread-6] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2018-09-08 16:25:55.549 INFO 2584 --- [ Thread-6] com.gyc.bean.CustomBean : CustomBean-destroy 2018-09-08 16:25:55.549 INFO 2584 --- [ Thread-6] com.gyc.bean.LifeCycleBean : LifeCycleBean-destroy 2018-09-08 16:25:55.549 INFO 2584 --- [ Thread-6] com.gyc.bean.AnnotationBean : AnnotationBean-destroy
梳理了bean的生命周期,大概明白了spring对bean的一个处理流程;
对最上边的图进行详细描述: