春天中的bean相关的生命周期

  介绍

这篇文章主要讲解了春天中的bean相关的生命周期,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

记得以前的时候,每次提中起春天的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚:什么beanFactory啊,知道接口啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。

今天终于理清这些关系了,并且又新增了对postConstruct和生命周期的理解。

——首先是BeanFactoryPostProcessor,它是针对所有的bean的定义,只执行一次

下面是针对每个bean的初始

    <李>,实现了一系列清楚接口的,比如BeanNameAware, ApplicationContextAware,调用其集方法李 <李> -执行beanPostProcessor的postProcessBeforeInitialization方法李 <李> -带有@PostConstruct注解的方法李 <李> -实现InitializingBean接口的afterPropertiesSet方法李 <李> -指定的initMethod方法李 <李> -执行beanPostProcessor的postProcessAfterInitialization方法李 <李> -实现了SmartLifecycle接口的开始方法(实现生命周期接口的不会自动调用,需要显式的调用开始方法)

下面是针对每个bean的销毁

    <李>,实现了SmartLifecycle接口的停止方法(实现生命周期接口的不会自动调用,需要显式的调用停止方法)李 <李> -带有@PreDestroy注解的方法李 <李> -实现DisposableBean接口的破坏方法李 <李> -指定的destroyMethod方法

目前就想到这么多了,其他的麻烦在评论区留言呀~

<强>代码实例

<强>豆实体类

/* *   * @date: 2020-07-22   *   *一个简单的枚举类   */公共enum BeanType {   正常,生命周期,SMART_LIFECYCLE;   }      进口javax.annotation.PostConstruct;   进口javax.annotation.PreDestroy;   进口lombok.extern.slf4j.Slf4j;   进口org.springframework.beans.BeansException;   进口org.springframework.beans.factory.BeanNameAware;   进口org.springframework.beans.factory.DisposableBean;   进口org.springframework.beans.factory.InitializingBean;   进口org.springframework.context.ApplicationContext;   进口org.springframework.context.ApplicationContextAware;/* *   * @author:并   * @date: 2020-07-22   *一个简单的bean   */@Slf4j   公共类NormalBean实现BeanNameAware、ApplicationContextAware InitializingBean DisposableBean {   私人BeanType BeanType;      公共NormalBean () {   这(BeanType.NORMAL);   }      公共NormalBean (BeanType BeanType) {   这一点。beanType=beanType;   }      @PostConstruct   公共空间postConstruct () {   log.info (“{}, postConstruct" beanType);   }      @Override   公共空间afterPropertiesSet()抛出异常{   log.info (“{}, afterPropertiesSet" beanType);   }      公共空间initMethod () {   log.info (“{}, initMethod" beanType);   }      @PreDestroy   公共空间preDestroy () {   log.info (“{}, preDestroy" beanType);   }      @Override   公共空间摧毁()抛出异常{   log.info (“{}, destroy" beanType);   }      公共空间destroyMethod () {   log.info (“{}, destroyMethod" beanType);   }      @Override   公共空间setApplicationContext (ApplicationContext ApplicationContext)抛出BeansException {   log.info (“setApplicationContext, applicationContext: {}“, applicationContext);   }      @Override   公共空间setBeanName(字符串名称){   log.info (“setBeanName, bean名称:{}“,名称);   }   }      进口lombok.extern.slf4j.Slf4j;   进口org.springframework.context.Lifecycle;/* *   * @author:并   * @date: 2020-07-22   *实现了生命周期的一个bean   */@Slf4j   公共类LifecycleBean NormalBean延伸实现生命周期{   私人动荡的布尔运行=false;      公共LifecycleBean () {   超级(BeanType.LIFECYCLE);   }      @Override   公共空间开始(){   log.info(“时);   运行=true;   }      @Override   公共空间停止(){   log.info (“stop");   运行=false;   }      @Override   公共布尔正在(){   返回运行;   }   }      进口lombok.extern.slf4j.Slf4j;   进口org.springframework.context.SmartLifecycle;/* *   * @author:并   * @date: 2020-07-22   *实现了SmartLifecycle的一个bean   */@Slf4j   公共类SmartLifecycleBean NormalBean延伸实现SmartLifecycle {   私人动荡的布尔运行=false;      公共SmartLifecycleBean () {   超级(BeanType.SMART_LIFECYCLE);   }      @Override   公共空间开始(){   log.info(“时);   运行=true;   }      @Override   公共空间停止(){   log.info (“stop");   运行=false;   }      @Override   公共布尔正在(){   返回运行;   }   }

春天中的bean相关的生命周期