一文搞懂Spring中Bean的生命周期
生命周期:從創(chuàng)建到消亡的完整過(guò)程
bean聲明周期:bean從創(chuàng)建到銷(xiāo)毀的整體過(guò)程
bean聲明周期控制:在bean創(chuàng)建后到銷(xiāo)毀前做一些事情
一、使用配置生命周期的方法
在BookDaoImpl中實(shí)現(xiàn)類(lèi)中創(chuàng)建相應(yīng)的方法:
//表示bean初始化對(duì)應(yīng)的操作 public void init(){ System.out.println("init..."); } //表示bean銷(xiāo)毀前對(duì)應(yīng)的操作 public void destory(){ System.out.println("destory..."); }
applicationContext.xml配置初始化聲明周期回調(diào)函數(shù)及銷(xiāo)毀聲明周期回調(diào)函數(shù)
<!--init-method:設(shè)置bean初始化生命周期回調(diào)函數(shù)--> <!--destroy-method:設(shè)置bean銷(xiāo)毀生命周期回調(diào)函數(shù),僅適用于單例對(duì)象--> <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" init-method="init" destroy-method="destory"/>
執(zhí)行結(jié)果:
虛擬機(jī)退出,沒(méi)有給bean銷(xiāo)毀的機(jī)會(huì)。
可利用ClassPathXmlApplictionContext里的close方法主動(dòng)關(guān)閉容器,就會(huì)執(zhí)行銷(xiāo)毀方法。
import com.itheima.dao.BookDao; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppForLifeCycle { public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); BookDao bookDao = (BookDao) ctx.getBean("bookDao"); bookDao.save(); //關(guān)閉容器 ctx.close(); } }
執(zhí)行結(jié)果:
不過(guò)這種方式比較暴力,容器還提供另外的方法
在AppForLifeCycle中用關(guān)閉鉤子函數(shù)
//注冊(cè)關(guān)閉鉤子函數(shù),在虛擬機(jī)退出之前回調(diào)此函數(shù),關(guān)閉容器 ctx.registerShutdownHook();
執(zhí)行結(jié)果:
關(guān)閉鉤子在任何時(shí)間都可以執(zhí)行,close關(guān)閉比較暴力。
二、生命周期控制——接口控制(了解)
applicationContext.xml配置:
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"> <property name="bookDao" ref="bookDao"/> </bean>
BookServiceImpl:
可以利用接口InitializingBean和DisposableBean來(lái)設(shè)置初始化和銷(xiāo)毀后的方法設(shè)置
import com.itheima.dao.BookDao; import com.itheima.service.BookService; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class BookServiceImpl implements BookService, InitializingBean, DisposableBean { private BookDao bookDao; public void setBookDao(BookDao bookDao) { System.out.println("set ....."); this.bookDao = bookDao; } public void save() { System.out.println("book service save ..."); bookDao.save(); } public void destroy() throws Exception { System.out.println("service destroy"); } public void afterPropertiesSet() throws Exception { System.out.println("service init"); } }
執(zhí)行結(jié)果:
可以看出set在執(zhí)行在init的執(zhí)行之后,當(dāng)你的屬性設(shè)置完以后,才去執(zhí)行afterPropertiesSet,所有才叫afterPropertiesSet,在屬性設(shè)置之后。
小結(jié)
生命周期總結(jié)
初始化容器
- 1、創(chuàng)建對(duì)象
- 2、執(zhí)行構(gòu)造方法
- 3、執(zhí)行屬性注入(set操作)
- 4、執(zhí)行bean初始化方法
使用bean
執(zhí)行業(yè)務(wù)操作
關(guān)閉/銷(xiāo)毀容器
執(zhí)行bean操作
1、bean生命周期控制
配置
init-method
destroy-method
接口(了解)
InitializingBean
DisposableBean
2、關(guān)閉容器
ConfigurableApplicationContext
close()
registerShutdownHook()
到此這篇關(guān)于一文搞懂Spring中Bean的生命周期的文章就介紹到這了,更多相關(guān)Spring Bean生命周期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解Java class文件格式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
對(duì)于理解JVM和深入理解Java語(yǔ)言, 學(xué)習(xí)并了解class文件的格式都是必須要掌握的功課2017-06-06Mybatis-plus通用查詢(xún)方法封裝的實(shí)現(xiàn)
本文主要介紹了Mybatis-plus通用查詢(xún)方法封裝的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07詳解配置類(lèi)為什么要添加@Configuration注解
這篇文章主要介紹了詳解配置類(lèi)為什么要添加@Configuration注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Java并發(fā)編程中使用Executors類(lèi)創(chuàng)建和管理線程的用法
這篇文章主要介紹了Java并發(fā)編程中使用Executors類(lèi)創(chuàng)建和管理線程的用法,文中舉了用其啟動(dòng)線程和設(shè)置線程優(yōu)先級(jí)的例子,需要的朋友可以參考下2016-03-03詳解SpringCloud-Alibaba-Seata分布式事務(wù)
這篇文章主要介紹了SpringCloud-Alibaba-Seata分布式事務(wù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12