新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)Spring Bean的生命周期介紹,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注于安岳企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城網(wǎng)站開(kāi)發(fā)。安岳網(wǎng)站建設(shè)公司,為安岳等地區(qū)提供建站服務(wù)。全流程按需定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
spring的生命周期其實(shí)是一個(gè)很簡(jiǎn)單的過(guò)程,從配置文件一開(kāi)始,它就會(huì)針對(duì)你的這個(gè)bean做一系列的操作。往往在剛開(kāi)始的時(shí)候很難從理論方面深入理解它,也許通過(guò)代碼實(shí)例,你將能夠徹底了解它。
1. BeanPostProcessor后置處理器
BeanPostProcessor【interface】:
bean的后置處理器:
在bean初始化前后進(jìn)行一些處理工作。
?1. postProcessBeforeInitialization:在初始化之前工作
?2. postProcessAfterInitialization:在初始化之后工作
/** * 后置處理器,初始化前后進(jìn)行處理工作 */@Componentpublic class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization....."+beanName+"=>"+bean); return bean;//可對(duì)bean進(jìn)行包裝后返回 } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization....."+beanName+"=>"+bean); return bean;//可對(duì)bean進(jìn)行包裝后返回 }}
2. @Bean初始化和銷毀
1.1 bean的生命周期:
??bean創(chuàng)建-----初始化-----銷毀的過(guò)程
1.2 容器管理bean的生命周期:
??我們可以自定義初始化和銷毀方法:容器在bean進(jìn)行到當(dāng)前生命周期的時(shí)候來(lái)調(diào)用我們自定義的初始化和銷毀方法。
構(gòu)造函數(shù):
?單實(shí)例:容器創(chuàng)建時(shí)進(jìn)行初始化
?多實(shí)例:在每次獲取的時(shí)候創(chuàng)建對(duì)象
BeanPostProcessor.postProcessBeforeInitialization
初始化:
?對(duì)象創(chuàng)建完成,并賦值好,調(diào)用初始化方法。BeanPostProcessor.postProcessAfterInitialization
銷毀:
?單實(shí)例:容器關(guān)閉的時(shí)候
?多實(shí)例:容器不會(huì)管理這個(gè)bean;容器不會(huì)調(diào)用銷毀方法;
1、指定初始化和銷毀方法
?通過(guò)@Bean指定init-method和destroy-method;
2、通過(guò)Bean實(shí)現(xiàn)InitializingBean(定義初始化邏輯)
?DisposableBean(定義銷毀邏輯)
3、可以使用JSR250規(guī)范:
?@PostConstruct:在bean創(chuàng)建完成并且屬性賦值完成
?@PreDestroy:在容器銷毀bean銷毀之前調(diào)用清理工作
代碼如下:
a. initMethod 和destroyMethod 的使用
MainConfig.java
@Configuration@ComponentScan("com.zero.life")public class MainConfig {// @Scope("prototype") @Bean(initMethod = "init",destroyMethod = "destroy") public Phone phone(){ return new Phone(); }} Phone.java public class Phone { public Phone() { System.out.println("Phone初始化構(gòu)造。。。"); } public void init(){ System.out.println("Phone 初始化方法。。。。"); } public void destroy(){ System.out.println("Phone 銷毀方法。。。"); }}
b. InitializingBean和DisposableBean 的使用
@Componentpublic class Android implements InitializingBean,DisposableBean { public Android() { System.out.println("android constructor......."); } @Override public void destroy() throws Exception { System.out.println("android destroy........"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("android afterPropertiesSet........"); }}
c. @PostConstruct和@PreDestroy的使用
@Componentpublic class AIIphone { public AIIphone() { System.out.println("AIIphone.... contruct..."); } @PostConstruct public void init(){ System.out.println("AIIphone.....PostConstruct"); } @PreDestroy public void destroy(){ System.out.println("AIIphone......PreDestroy"); }}
關(guān)于Spring Bean的生命周期介紹就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享標(biāo)題:SpringBean的生命周期介紹
網(wǎng)站路徑:http://www.ef60e0e.cn/article/jhcpid.html