最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中的Bean的初始化與銷毀順序解析

 更新時(shí)間:2021年08月13日 09:49:46   作者:qq_41075649  
這篇文章主要介紹了SpringBoot中的Bean的初始化與銷毀順序,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

我今天學(xué)習(xí)到SpringBoot里面自定義Bean的初始化與銷毀方法

我先總結(jié)一下我學(xué)到的四種方法:

方法一:

指定init-method 和 destory-method

方法二:

通過讓 Bean 實(shí)現(xiàn) InitializingBean 接口,定義初始化邏輯

DisposableBean 接口,定義銷毀邏輯

方法三:

用 @PostConstruct,在 Bean 創(chuàng)建完成并且賦值完成后,執(zhí)行該注解標(biāo)注的方法

@PreDestroy,在容器銷毀 Bean 之前,執(zhí)行該注解標(biāo)注的方法

方法四:

通過讓 Bean 實(shí)現(xiàn) BeanPostProcessor 接口,在Bean 初始化前后進(jìn)行一些處理工作

  • postProcessBeforeInitialization: 在初始化之前工作
  • postProcessAfterInitialization: 在初始化之后工作

然后我就在想它們的執(zhí)行順序是怎樣的:

嘗試一:

配置類:

//告訴Spring這是一個(gè)配置類
@Configuration
public class MainConfigOfLifeCycle {
 
        //利用 init-method 和 destory-method
 @Bean(initMethod="initTest", destroyMethod="detoryTest")
 public Car car() {
  return new Car();
 }
 
        //實(shí)現(xiàn) InitializingBean , DisposableBean 接口
 @Bean
 public Cat cat() {
  return new Cat();
 }
 
        //利用 @PostConstruct ,@PreDestroy
 @Bean
 public Dog dog() {
  return new Dog();
 }
 
        //實(shí)現(xiàn) BeanPostProcessor 接口
 @Bean
 public MyBeanPostProcessor myBeanPostProcessor() {
  return new MyBeanPostProcessor();
 } 
}

4個(gè) bean:

public class Car{ 
 public void initTest() {
  System.out.println(" .. init-method .. ");
 }
 
 public void detoryTest() {
  System.out.println(" .. destory-method .. ");
 }
}
public class Cat implements InitializingBean, DisposableBean {
 
 //該Bean在銷毀時(shí),調(diào)用
 public void destroy() throws Exception {
  // TODO Auto-generated method stub  
  System.out.println(" .. DisposableBean ..");
 }
 
 //該Bean創(chuàng)建完成并且賦值完成后,調(diào)用
 public void afterPropertiesSet() throws Exception {
  // TODO Auto-generated method stub
  
  System.out.println(" .. InitializingBean ..");
 } 
}
public class Dog { 
 //對象創(chuàng)建并賦值之后調(diào)用
 @PostConstruct
 public void init() {
  System.out.println(" .. @PostConstruct .. ");
 }
 
 //容器移除對象之前
 @PreDestroy
 public void detory() {
  System.out.println(" .. @PreDestroy .. ");
 } 
}
public class MyBeanPostProcessor implements BeanPostProcessor {
 
 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  // TODO Auto-generated method stub
  
  System.out.println(" .. postProcessBeforeInitialization .. ");  
  return bean;
 }
 
 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  // TODO Auto-generated method stub
  
  System.out.println(" .. postProcessBeforeInitialization .. ");  
  return bean;
 } 
}

運(yùn)行:

public class IOCTest_LifeCycle { 
 @Test
 public void test01() {
  // 1. 創(chuàng)建IOC容器
  AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
  System.out.println("容器創(chuàng)建完成");
 
  // 關(guān)閉容器
  applicationContext.close();
 } 
}

執(zhí)行結(jié)果:

思考:發(fā)現(xiàn)容器在加載 Bean 時(shí)是順序的,因?yàn)槲以贛ainConfigOfLifeCycle這個(gè)配置類里 @Bean 是順序的,所有不能確定這次結(jié)果是否準(zhǔn)確。

嘗試二:我把上面四種方法都雜糅到一個(gè)類里

配置類:

@Configuration
public class MainConfigOfLifeCycle { 
 @Bean(initMethod="initTest", destroyMethod="detoryTest")
 public Car car() {
  return new Car();
 }
}

Bean:

public class Car implements InitializingBean, DisposableBean, BeanPostProcessor { 
 public Car() {
  System.out.println("Car 創(chuàng)建");
 }
 
 public void initTest() {
  System.out.println(" .. init-method .. ");
 }
 
 public void detoryTest() {
  System.out.println(" .. destory-method .. ");
 }
 
 @Override
 public void afterPropertiesSet() throws Exception {
  System.out.println(" .. InitializingBean .. ");
 }
 
 @Override
 public void destroy() throws Exception {
  System.out.println(" .. DisposableBean .. ");
 } 
 
 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  System.out.println(" .. postProcessBeforeInitialization .. "); 
  return bean;
 }
 
 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  System.out.println(" .. postProcessAfterInitialization .. "); 
  return bean;
 }
 
 @PostConstruct
 public void postConstructTest() {
  System.out.println(" .. @PostConstruct .. ");
 }
 
 @PreDestroy
 public void preDestroyTest() {
  System.out.println(" .. @PreDestroy .. ");
 } 
}

運(yùn)行:

public class IOCTest_LifeCycle { 
 @Test
 public void test01() {
  // 1. 創(chuàng)建IOC容器
  AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
  System.out.println("容器創(chuàng)建完成");
 
  // 關(guān)閉容器
  applicationContext.close();
 } 
}

執(zhí)行結(jié)果:

總結(jié):

創(chuàng)建:

  • Bean 構(gòu)造函數(shù) ——> @PostConstruct ——> InitializingBean 接口 ——> bean 定義的 init-method——> postProcessBeforeInitialization ——> Bean 初始化完成 ——> postProcessAfterInitialization ——> 容器創(chuàng)建完成

銷毀:

  • @PreDestroy ——> DisposableBean 接口 ——> bean 定義的 destoryMethod ——> Bean銷毀

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

芮城县| 冕宁县| 乌拉特中旗| 武山县| 晴隆县| 永新县| 巴彦县| 高州市| 类乌齐县| 白玉县| 大埔区| 北安市| 左权县| 永年县| 天门市| 平山县| 平度市| 盐池县| 苏州市| 嘉兴市| 屯门区| 海晏县| 庆阳市| 双峰县| 寻甸| 阿克陶县| 靖宇县| 本溪| 剑阁县| 山东省| 定南县| 安陆市| 克拉玛依市| 自治县| 内丘县| 兴义市| 南昌县| 威远县| 渑池县| 西畴县| 乐都县|