亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring中的bean相關的生命周期

發布時間:2020-07-27 13:52:00 來源:億速云 閱讀:249 作者:小豬 欄目:開發技術

這篇文章主要講解了Spring中的bean相關的生命周期,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

前言

記得以前的時候,每次提起Spring中的bean相關的生命周期時,內心都無比的恐懼,因為好像有很多,自己又理不清楚:什么beanFactory啊,aware接口啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。

今天終于理清這些關系了,并且又新增了對postConstruct和lifecycle的理解。

執行順序

- 首先是 BeanFactoryPostProcessor,它是針對所有bean的definition的,只執行一次

下面是針對每個bean的初始

  • - 實現了一系列aware接口的,比如BeanNameAware,ApplicationContextAware,調用其set方法
  • - 執行BeanPostProcessor的postProcessBeforeInitialization方法
  • - 帶有@PostConstruct注解的方法
  • - 實現InitializingBean接口的afterPropertiesSet方法
  • - 指定的initMethod方法
  • - 執行BeanPostProcessor的postProcessAfterInitialization方法
  • - 實現了SmartLifecycle接口的start方法(實現Lifecycle接口的不會自動調用,需要顯式的調用start方法)

下面是針對每個bean的銷毀

  • - 實現了SmartLifecycle接口的stop方法(實現Lifecycle接口的不會自動調用,需要顯式的調用stop方法)
  • - 帶有@PreDestroy注解的方法
  • - 實現DisposableBean接口的destroy方法
  • - 指定的destroyMethod方法

目前就想到這么多了,其他的麻煩在評論區留言呀~

代碼實例

bean實體類

/**
 * @date: 2020-07-22
 * 
 * 一個簡單的枚舉類 
 */
public enum BeanType {
  NORMAL, LIFECYCLE, SMART_LIFECYCLE;
}

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 * @author: lihui
 * @date: 2020-07-22
 * 一個簡單的bean
 */
@Slf4j
public class NormalBean implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
  private BeanType beanType;

  public NormalBean() {
    this(BeanType.NORMAL);
  }

  public NormalBean(BeanType beanType) {
    this.beanType = beanType;
  }

  @PostConstruct
  public void postConstruct() {
    log.info("{}, postConstruct", beanType);
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    log.info("{}, afterPropertiesSet", beanType);
  }

  public void initMethod() {
    log.info("{}, initMethod", beanType);
  }

  @PreDestroy
  public void preDestroy() {
    log.info("{}, preDestroy", beanType);
  }

  @Override
  public void destroy() throws Exception {
    log.info("{}, destroy", beanType);
  }

  public void destroyMethod() {
    log.info("{}, destroyMethod", beanType);
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    log.info("setApplicationContext, applicationContext : {}", applicationContext);
  }

  @Override
  public void setBeanName(String name) {
    log.info("setBeanName, bean name : {}", name);
  }
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.Lifecycle;
/**
 * @author: lihui
 * @date: 2020-07-22
 * 實現了Lifecycle的一個bean
 */
@Slf4j
public class LifecycleBean extends NormalBean implements Lifecycle {
  private volatile boolean running = false;

  public LifecycleBean() {
    super(BeanType.LIFECYCLE);
  }

  @Override
  public void start() {
    log.info("start");
    running = true;
  }

  @Override
  public void stop() {
    log.info("stop");
    running = false;
  }

  @Override
  public boolean isRunning() {
    return running;
  }
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.SmartLifecycle;
/**
 * @author: lihui
 * @date: 2020-07-22
 * 實現了SmartLifecycle的一個bean
 */
@Slf4j
public class SmartLifecycleBean extends NormalBean implements SmartLifecycle {
  private volatile boolean running = false;

  public SmartLifecycleBean() {
    super(BeanType.SMART_LIFECYCLE);
  }

  @Override
  public void start() {
    log.info("start");
    running = true;
  }

  @Override
  public void stop() {
    log.info("stop");
    running = false;
  }

  @Override
  public boolean isRunning() {
    return running;
  }
}

配置類

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
/**
 * @author: lihui
 * @date: 2020-07-25
 */
@Slf4j
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    log.info("postProcessBeanFactory, beanFactory:{}", beanFactory);
  }
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
 * @author: lihui
 * @date: 2020-07-25
 */
@Slf4j
public class MyBeanPostProcessor implements BeanPostProcessor {
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    log.info("postProcessBeforeInitialization, bean:{}", beanName);
    return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    log.info("postProcessAfterInitialization, bean:{}", beanName);
    return bean;
  }
}

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author: lihui
 * @date: 2020-07-22
 */
@Configuration
@Slf4j
public class Config {

  @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
  public NormalBean normalBean() {
    return new NormalBean();
  }

  @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
  public LifecycleBean lifecycleBean() {
    return new LifecycleBean();
  }

  @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
  public SmartLifecycle smartLifecycle() {
    return new SmartLifecycleBean();
  }

  @Bean
  public static MyBeanFactoryPostProcessor myBeanFactoryPostProcessor() {
    return new MyBeanFactoryPostProcessor();
  }

  @Bean
  public static MyBeanPostProcessor myBeanPostProcessor() {
    return new MyBeanPostProcessor();
  }
}

Main類

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author: lihui
 * @date: 2020-07-22
 */
@Slf4j
public class Main {
  public static void main(String[] args) throws InterruptedException {
    ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    ctx.registerShutdownHook();
    Thread.sleep(5000);
    log.info("line ----------------------------- line");
    ctx.start();
    ctx.stop();
    log.info("line ----------------------------- line");
  }
}

結果說明

結果正如前面所說的執行順序一致,主要注意的就是Lifecycle接口和SmartLifecycle接口,只有實現了SmartLifecycle接口的bean在初始化時才會被自動調用,而實現了Lifecycle接口的除非顯式調用start和stop方法才會被調用。

看完上述內容,是不是對Spring中的bean相關的生命周期有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

通化县| 习水县| 溆浦县| 穆棱市| 城步| 定州市| 禄劝| 平利县| 太康县| 崇州市| 乌审旗| 壤塘县| 临猗县| 万源市| 双辽市| 柳林县| 积石山| 黄陵县| 沽源县| 遂宁市| 娱乐| 循化| 潜江市| 开平市| 长寿区| 若尔盖县| 巨野县| 景东| 五常市| 疏附县| 普洱| 德令哈市| 丘北县| 鄄城县| 交城县| 化隆| 琼结县| 额尔古纳市| 黔西| 鄄城县| 四平市|