日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring5 - Bean的初始化和销毁的4种方式

發布時間:2025/3/21 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring5 - Bean的初始化和销毁的4种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 方式一: 自行指定bean的初始化方法和bean的銷毀方法
  • 方式二: 通過 InitializingBean和DisposableBean 接口實現bean的初始化以及銷毀方法
  • 方式三: 通過JSR250規范 提供的注解@PostConstruct 和@ProDestory標注的方法
  • 方式四:通過Spring的BeanPostProcessor的 bean的后置處理器會攔截所有bean創建過程

概述

  • 針對單實例bean的話,容器啟動的時候,bean的對象就創建了,而且容器銷毀的時候,也會調用Bean的銷毀方法
  • 針對原型bean的話,容器啟動的時候,bean是不會被創建的而是在獲取bean的時候被創建,而且bean的銷毀不受 IOC容器的管理.

方式一: 自行指定bean的初始化方法和bean的銷毀方法

【beans】

package com.artisan.base.lifeCycle;public class A1 {public void init(){System.out.println("A1 init");}public void destory(){System.out.println("A1 destory");} } package com.artisan.base.lifeCycle;public class A2 {public void init(){System.out.println("A2 init");}public void destory(){System.out.println("A2 destory");} }

【配置類】

package com.artisan.base.lifeCycle;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope;@Configuration public class LCConfig {@Bean(initMethod = "init",destroyMethod = "destory")public A1 a1(){return new A1();}@Scope("prototype")@Beanpublic A2 a2(){return new A2();} }

【測試】

package com.artisan.base.lifeCycle;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.concurrent.TimeUnit;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/10/11 22:58* @mark: show me the code , change the world*/ public class LCTest {public static void main(String[] args) throws InterruptedException {AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(LCConfig.class);TimeUnit.SECONDS.sleep(3);// 容器銷毀ac.close();} }


方式二: 通過 InitializingBean和DisposableBean 接口實現bean的初始化以及銷毀方法

package com.artisan.base.lifeCycle;import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/10/11 23:06* @mark: show me the code , change the world*/ public class A3 implements InitializingBean, DisposableBean {public A3() {System.out.println("A3 init");}@Overridepublic void destroy() throws Exception {System.out.println("A3 destroy");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("A3 afterPropertiesSet");} }

執行上面的測試代碼


方式三: 通過JSR250規范 提供的注解@PostConstruct 和@ProDestory標注的方法

package com.artisan.base.lifeCycle;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2020/10/11 23:12* @mark: show me the code , change the world*/ public class A4 {public A4() {System.out.println("A4 Construct ");}@PostConstructpublic void init(){System.out.println("A4 init");}@PreDestroypublic void destory(){System.out.println("A4 destory");} }

測試結果


方式四:通過Spring的BeanPostProcessor的 bean的后置處理器會攔截所有bean創建過程

public class A5 {public A5() {System.out.println("A5 Construct");}public void init(){System.out.println("A5 init");}public void destory(){System.out.println("A5 destory");} }

【bean後置處理器】

package com.artisan.base.lifeCycle;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor;public class A5BeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof A5){System.out.println("A5BeanPostProcessor...postProcessBeforeInitialization:"+beanName);}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof A5){System.out.println("A5BeanPostProcessor...postProcessAfterInitialization:"+beanName);}return bean;} }

【config】

【test】

簡單說一下 BeanPostProcessor的執行時機


看看invokeInitMethods


總結

以上是生活随笔為你收集整理的Spring5 - Bean的初始化和销毁的4种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。