一文搞懂Spring中Bean的生命周期
生命周期:從創(chuàng)建到消亡的完整過程
bean聲明周期:bean從創(chuàng)建到銷毀的整體過程
bean聲明周期控制:在bean創(chuàng)建后到銷毀前做一些事情
一、使用配置生命周期的方法
在BookDaoImpl中實(shí)現(xiàn)類中創(chuàng)建相應(yīng)的方法:
//表示bean初始化對應(yīng)的操作
public void init(){
System.out.println("init...");
}
//表示bean銷毀前對應(yīng)的操作
public void destory(){
System.out.println("destory...");
}
applicationContext.xml配置初始化聲明周期回調(diào)函數(shù)及銷毀聲明周期回調(diào)函數(shù)
<!--init-method:設(shè)置bean初始化生命周期回調(diào)函數(shù)-->
<!--destroy-method:設(shè)置bean銷毀生命周期回調(diào)函數(shù),僅適用于單例對象-->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" init-method="init" destroy-method="destory"/>
執(zhí)行結(jié)果:

虛擬機(jī)退出,沒有給bean銷毀的機(jī)會(huì)。
可利用ClassPathXmlApplictionContext里的close方法主動(dòng)關(guān)閉容器,就會(huì)執(zhí)行銷毀方法。
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é)果:

不過這種方式比較暴力,容器還提供另外的方法
在AppForLifeCycle中用關(guān)閉鉤子函數(shù)
//注冊關(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來設(shè)置初始化和銷毀后的方法設(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)建對象
- 2、執(zhí)行構(gòu)造方法
- 3、執(zhí)行屬性注入(set操作)
- 4、執(zhí)行bean初始化方法
使用bean
執(zhí)行業(yè)務(wù)操作
關(guān)閉/銷毀容器
執(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解Java class文件格式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
對于理解JVM和深入理解Java語言, 學(xué)習(xí)并了解class文件的格式都是必須要掌握的功課2017-06-06
Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn)
本文主要介紹了Mybatis-plus通用查詢方法封裝的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法
這篇文章主要介紹了Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法,文中舉了用其啟動(dòng)線程和設(shè)置線程優(yōu)先級的例子,需要的朋友可以參考下2016-03-03
詳解SpringCloud-Alibaba-Seata分布式事務(wù)
這篇文章主要介紹了SpringCloud-Alibaba-Seata分布式事務(wù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

