亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot中Bean生命周期自定義初始化和銷毀方法詳解

 更新時間:2024年01月24日 09:11:49   作者:浩澤學(xué)編程  
這篇文章給大家詳細介紹了SpringBoot中Bean生命周期自定義初始化和銷毀方法,文中通過代碼示例講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

一、@Bean注解指定初始化和銷毀方法

創(chuàng)建BeanTest類,自定義初始化方法和銷毀方法。在@Bean注解的參數(shù)中指定BeanTest自定義的初始化和銷毀方法:銷毀方法只有在IOC容器關(guān)閉的時候才調(diào)用。

代碼如下:

/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: dog
 * @Description: TODO描述
 * @Date: 2024/1/21 22:55
 */
public class BeanTest {
    public BeanTest(){
        System.out.println("BeanTest被創(chuàng)建");
    }

    public void init(){
        System.out.println("BeanTest被初始化");
    }

    public void destory(){
        System.out.println("BeanTest被銷毀");
    }
}
/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: MyConfig
 * @Description: TODO描述
 * @Date: 2024/1/21 22:59
 */
@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {
    @Bean(initMethod = "init",destroyMethod = "destory")
    public BeanTest beanTest(){
        return new BeanTest();
    }
}
//測試代碼
AnnotationConfigApplicationContext ct = new AnnotationConfigApplicationContext(MyConfig.class);
System.out.println("IoC容器創(chuàng)建完成");

在這里插入圖片描述

  • 可以看到調(diào)用的是自定義的方法,這里解釋一下,測試時,運行完代碼塊程序就結(jié)束了,所喲IoC容器就被關(guān)閉,所以調(diào)用了IoC銷毀方法。同時可以看到初始化方法在對象創(chuàng)建完成后調(diào)用。
  • 當(dāng)組件的作用域為單例時在容器啟動時即創(chuàng)建對象,而當(dāng)作用域為原型(PROTOTYPE)時在每次獲取對象的時候才創(chuàng)建對象。并且當(dāng)作用域為原型(PROTOTYPE)時,IOC容器只負責(zé)創(chuàng)建Bean但不會管理Bean,所以IOC容器不會調(diào)用銷毀方法。

二、實現(xiàn)InitializingBean接口和DisposableBean接口

看一下兩接口的方法:

public interface InitializingBean {

	/**
	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
	 * <p>This method allows the bean instance to perform validation of its overall
	 * configuration and final initialization when all bean properties have been set.
	 * @throws Exception in the event of misconfiguration (such as failure to set an
	 * essential property) or if initialization fails for any other reason
	 * Bean都裝配完成后執(zhí)行初始化
	 */
	void afterPropertiesSet() throws Exception;
}
====================================================================
public interface DisposableBean {

	/**
	 * Invoked by the containing {@code BeanFactory} on destruction of a bean.
	 * @throws Exception in case of shutdown errors. Exceptions will get logged
	 * but not rethrown to allow other beans to release their resources as well.
	 */
	void destroy() throws Exception;

}

代碼如下:

/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: BeanTest1
 * @Description: TODO描述
 * @Date: 2024/1/21 23:25
 */
public class BeanTest1 implements InitializingBean, DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("BeanTest1銷毀");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("BeanTest1初始化");
    }

    public BeanTest1() {
        System.out.println("BeanTest1被創(chuàng)建");
    }
}
=========================

@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {
 @Bean
    public BeanTest1 beanTest1(){
        return new BeanTest1();
    }
}

在這里插入圖片描述

三、@PostConstruct(初始化邏輯)和@PreDestroy(銷毀邏輯)注解

  • 被@PostConstruct修飾的方法會在服務(wù)器加載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次,類似于Serclet的inti()方法。
  • 被@PostConstruct修飾的方法會在構(gòu)造函數(shù)之后,init()方法之前運行。
  • 被@PreDestroy修飾的方法會在服務(wù)器卸載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次,類似于Servlet的destroy()方法。被@PreDestroy修飾的方法會在destroy()方法之后運行,在Servlet被徹底卸載之前。

代碼如下:

/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: BeanTest2
 * @Description: TODO描述
 * @Date: 2024/1/21 23:32
 */
public class BeanTest2 {
    public BeanTest2(){
        System.out.println("BeanTest2被創(chuàng)建");
    }

    @PostConstruct
    public void init(){
        System.out.println("BeanTest2被初始化");
    }

    @PreDestroy
    public void destory(){
        System.out.println("BeanTest2被銷毀");
    }
}
========================
//
@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {
 @Bean
    public BeanTest2 beanTest2(){
        return new BeanTest2();
    }
}

在這里插入圖片描述

四、BeanPostProcessor接口

BeanPostProcessor又叫Bean的后置處理器,是Spring框架中IOC容器提供的一個擴展接口,在Bean初始化的前后進行一些處理工作。

BeanPostProcessor的源碼如下:

public interface BeanPostProcessor {

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 */
	@Nullable
	  //bean初始化方法調(diào)用前被調(diào)用
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
	 * instance and the objects created by the FactoryBean (as of Spring 2.0). The
	 * post-processor can decide whether to apply to either the FactoryBean or created
	 * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
	 * <p>This callback will also be invoked after a short-circuiting triggered by a
	 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
	 * in contrast to all other BeanPostProcessor callbacks.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 * @see org.springframework.beans.factory.FactoryBean
	 */
	@Nullable
	//bean初始化方法調(diào)用后被調(diào)用
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

代碼如下:

/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: MyBeanPostProcess
 * @Description: TODO描述
 * @Date: 2024/1/21 23:40
 */
@Component
public class MyBeanPostProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
        return bean;
    }
}
============================
@Configuration
@ComponentScan(("com.dragon.restart1"))
public class MyConfig {
    @Bean
    public BeanTest1 beanTest1(){
        return new BeanTest1();
    }

    @Bean
    public BeanTest2 beanTest2(){
        return new BeanTest2();
    }
}

運行結(jié)果如下:

BeanTest1被創(chuàng)建
postProcessBeforeInitialization...beanTest1=>com.dragon.restart1.BeanTest1@111d5c97
BeanTest1初始化
postProcessAfterInitialization...beanTest1=>com.dragon.restart1.BeanTest1@111d5c97
BeanTest2被創(chuàng)建
postProcessBeforeInitialization...beanTest2=>com.dragon.restart1.BeanTest2@29c17c3d
BeanTest2被初始化
postProcessAfterInitialization...beanTest2=>com.dragon.restart1.BeanTest2@29c17c3d
IoC容器創(chuàng)建完成
BeanTest2被銷毀
BeanTest1銷毀

通過上述運行結(jié)果可以發(fā)現(xiàn)使用BeanPostProcessor的運行順序為

IOC容器實例化Bean---->調(diào)用BeanPostProcessor的postProcessBeforeInitialization方法---->調(diào)用bean實例的初始化方法---->調(diào)用BeanPostProcessor的postProcessAfterInitialization方法。

總結(jié)

以上就是Bean生命周期自定義初始化和銷毀的講解。

到此這篇關(guān)于SpringBoot中Bean生命周期自定義初始化和銷毀方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot Bean初始化和銷毀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用Java搭建個簡單的Netty通信實例教程

    利用Java搭建個簡單的Netty通信實例教程

    這篇文章主要給大家介紹了關(guān)于如何利用Java搭建個簡單的Netty通信,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 分布式Netty源碼分析EventLoopGroup及介紹

    分布式Netty源碼分析EventLoopGroup及介紹

    這篇文章主要介紹了分布式Netty源碼分析EventLoopGroup及介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • 關(guān)于Mybatis-Plus?Update更新策略問題

    關(guān)于Mybatis-Plus?Update更新策略問題

    這篇文章主要介紹了關(guān)于Mybatis-Plus?Update更新策略問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • ByteArrayInputStream簡介和使用_動力節(jié)點Java學(xué)院整理

    ByteArrayInputStream簡介和使用_動力節(jié)點Java學(xué)院整理

    ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。這篇文章主要介紹了ByteArrayInputStream簡介和使用_動力節(jié)點Java學(xué)院整理,需要的朋友可以參考下
    2017-05-05
  • springcloud如何用Redlock實現(xiàn)分布式鎖

    springcloud如何用Redlock實現(xiàn)分布式鎖

    本文主要介紹了springcloud如何用Redlock實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • SpringCloud?分布式鎖的多種實現(xiàn)

    SpringCloud?分布式鎖的多種實現(xiàn)

    本文主要介紹了SpringCloud?分布式鎖的多種實現(xiàn),主要有三種方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • java接口性能優(yōu)化技巧

    java接口性能優(yōu)化技巧

    這篇文章主要為大家介紹了java接口性能優(yōu)化技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • java中的正則操作方法總結(jié)

    java中的正則操作方法總結(jié)

    關(guān)于正則表達式的使用,更多的是自己的經(jīng)驗,有興趣可以參閱相關(guān)書籍。這里主要寫一下java中的正則操作方法
    2013-10-10
  • Java內(nèi)部類詳解

    Java內(nèi)部類詳解

    內(nèi)部類在 Java 里面算是非常常見的一個功能了,在日常開發(fā)中我們肯定多多少少都用過,這里總結(jié)一下關(guān)于 Java 中內(nèi)部類的相關(guān)知識點和一些使用內(nèi)部類時需要注意的點。
    2020-02-02
  • SPRING管理XML方式過程解析

    SPRING管理XML方式過程解析

    這篇文章主要介紹了SPRING管理XML方式過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10

最新評論