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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Bean的生命周期——init-method和destroy-method - 通过让Bean实现InitializingBean,DisposableBean--BeanPostProcessor

發(fā)布時間:2025/4/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bean的生命周期——init-method和destroy-method - 通过让Bean实现InitializingBean,DisposableBean--BeanPostProcessor 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.



* bean的生命周期: * bean創(chuàng)建---初始化----銷毀的過程 * 容器管理bean的生命周期; * 我們可以自定義初始化和銷毀方法;容器在bean進行到當(dāng)前生命周期的時候來調(diào)用我們自定義的初始化和銷毀方法 * * 構(gòu)造(對象創(chuàng)建) * 單實例:在容器啟動的時候創(chuàng)建對象 * 多實例:在每次獲取的時候創(chuàng)建對象\ * * BeanPostProcessor.postProcessBeforeInitialization * 初始化: * 對象創(chuàng)建完成,并賦值好,調(diào)用初始化方法。。。 * BeanPostProcessor.postProcessAfterInitialization* 銷毀: * 單實例:容器關(guān)閉的時候 * 多實例:容器不會管理這個bean;容器不會調(diào)用銷毀方法;

Car.java

package com.dym.bean;public class Car {public Car(){System.out.println("car constructor...");}public void init(){System.out.println("car ... init...");}public void destroy(){System.out.println("car ... destroy...");}}

MainConfigOfLifeCycle.class

package com.dym.config;import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope;import com.dym.bean.Car;@Configuration public class MainConfigOfLifeCycle {//@Scope("prototype")@Bean(initMethod="init",destroyMethod="destroy")public Car car(){return new Car();}}

IOCTest_LifeCycle.class

package com.dym.test;import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.dym.config.MainConfigOfLifeCycle;public class IOCTest_LifeCycle {@Testpublic void test01(){//1、創(chuàng)建ioc容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);System.out.println("容器創(chuàng)建完成...");applicationContext.getBean("car");//關(guān)閉容器applicationContext.close();}}




* 1)、指定初始化和銷毀方法; * 通過@Bean指定init-method和destroy-method;* 2)、通過讓Bean實現(xiàn)InitializingBean(定義初始化邏輯), * DisposableBean(定義銷毀邏輯);* 3)、可以使用JSR250; * @PostConstruct:在bean創(chuàng)建完成并且屬性賦值完成;來執(zhí)行初始化方法 * @PreDestroy:在容器銷毀bean之前通知我們進行清理工作* 4)、BeanPostProcessor【interface】:bean的后置處理器; * 在bean初始化前后進行一些處理工作; * postProcessBeforeInitialization:在初始化之前工作 * postProcessAfterInitialization:在初始化之后工作

Cat.class

package com.dym.bean.dd;import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component;@Component public class Cat implements InitializingBean,DisposableBean {public Cat(){System.out.println("cat constructor...");}@Overridepublic void destroy() throws Exception {// TODO Auto-generated method stubSystem.out.println("cat...destroy...");}@Overridepublic void afterPropertiesSet() throws Exception {// TODO Auto-generated method stubSystem.out.println("cat...afterPropertiesSet...");} }



Dog.class

package com.dym.bean.dd;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component;@Component public class Dog implements ApplicationContextAware {//@Autowiredprivate ApplicationContext applicationContext;public Dog() {System.out.println("dog constructor...");}//對象創(chuàng)建并賦值之后調(diào)用@PostConstructpublic void init() {System.out.println("Dog....@PostConstruct...");}//容器移除對象之前@PreDestroypublic void detory() {System.out.println("Dog....@PreDestroy...");}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {// TODO Auto-generated method stubthis.applicationContext = applicationContext;} }



MyBeanPostProcessor.class

package com.dym.bean.dd;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component;/*** 后置處理器:初始化前后進行處理工作* 將后置處理器加入到容器中*/ @Component public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// TODO Auto-generated method stubSystem.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// TODO Auto-generated method stubSystem.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);return bean;} }



BeanPostProcessor原理

* 遍歷得到容器中所有的BeanPostProcessor;挨個執(zhí)行beforeInitialization, * 一旦返回null,跳出for循環(huán),不會執(zhí)行后面的BeanPostProcessor.postProcessorsBeforeInitialization * * BeanPostProcessor原理 * populateBean(beanName, mbd, instanceWrapper);給bean進行屬性賦值 * initializeBean * { * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); * invokeInitMethods(beanName, wrappedBean, mbd);執(zhí)行自定義初始化 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); *} * Spring底層對 BeanPostProcessor 的使用; * bean賦值,注入其他組件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;


總結(jié)

以上是生活随笔為你收集整理的Bean的生命周期——init-method和destroy-method - 通过让Bean实现InitializingBean,DisposableBean--BeanPostProcessor的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。