關(guān)于Java中Bean的生命周期詳解
?命周期
所謂的?命周期指的是?個(gè)對象從誕?到銷毀的整個(gè)?命過程,我們把這個(gè)過程就叫做?個(gè)對象的?命周期。
Bean 的?命周期分為以下 5 ?部分:
- 實(shí)例化 Bean(為 Bean 分配內(nèi)存空間)
- 設(shè)置屬性(Bean 注?和裝配)
- Bean 初始化
- 實(shí)現(xiàn)了各種 Aware 通知的?法,如 BeanNameAware、BeanFactoryAware、ApplicationContextAware 的接??法;
- 執(zhí)? BeanPostProcessor 初始化前置?法;
- 執(zhí)? @PostConstruct 初始化?法,依賴注?操作之后被執(zhí)?;
- 執(zhí)???指定的 init-method ?法(如果有指定的話);
- 執(zhí)? BeanPostProcessor 初始化后置?法。
- 使? Bean
- 銷毀 Bean
- 銷毀容器的各種?法,如 @PreDestroy、DisposableBean 接??法、destroy-method。
- 執(zhí)?流程如下圖所示:

實(shí)例化和初始化的區(qū)別
實(shí)例化和屬性設(shè)置是 Java 級別的系統(tǒng)“事件”,其操作過程不可???預(yù)和修改;
?初始化是給開發(fā)者提供的,可以在實(shí)例化之后,類加載完成之前進(jìn)??定義“事件”處理。
生命流程的“故事”
Bean 的?命流程看似繁瑣,但咱們可以以?活中的場景來理解它,?如我們現(xiàn)在需要買?棟房?,那么我們的流程是這樣的:
- 先買房(實(shí)例化,從?到有);
- 裝修(設(shè)置屬性);
- 買家電,如洗?機(jī)、冰箱、電視、空調(diào)等([各種]初始化);
- ?住(使? Bean);
- 賣出去(Bean 銷毀)。
生命周期演示
@Component
public class BeanLifeComponent implements BeanNameAware {
@PostConstruct
public void postConstruct() {
System.out.println("執(zhí)? PostConstruct()");
}
public void init() {
System.out.println("執(zhí)? BeanLifeComponent init-method");
}
@PreDestroy
public void preDestroy() {
System.out.println("執(zhí)?:preDestroy()");
}
public void setBeanName(String s) {
System.out.println("執(zhí)?了 setBeanName ?法:" + s);
}
}xml 配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:content="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <content:component-scan base-package="com.bit.component"></content:component-scan> <beans> <bean id="beanLifeComponent" class="com.bit.component.BeanLifeComponent" init-method="init"></bean> </beans> </beans>
調(diào)用類:
public class BeanLifeTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
BeanLife life = context.getBean(BeanLife.class);
System.out.println("執(zhí)? main ?法");
// 執(zhí)?銷毀?法
context.destroy();
}
}到此這篇關(guān)于關(guān)于Java中Bean的生命周期詳解的文章就介紹到這了,更多相關(guān)Bean的生命周期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA中實(shí)現(xiàn)springboot熱部署方式
在IDEA中實(shí)現(xiàn)SpringBoot的熱部署可以通過修改設(shè)置來完成,首先在設(shè)置中搜索Compiler,并勾選Build project automatically,然后進(jìn)入Advanced Settings,勾選Allow auto-make to start even if developed application is currently running2024-09-09
springboot+chatgpt+chatUI Pro開發(fā)智能聊天工具的實(shí)踐
本文主要介紹了springboot+chatgpt+chatUI Pro開發(fā)智能聊天工具的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
java實(shí)現(xiàn)微信小程序加密數(shù)據(jù)解密算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信小程序加密數(shù)據(jù)解密算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例
這篇文章主要介紹了Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例,幫助大家更好的理解和學(xué)習(xí)使用Spring Cloud,感興趣的朋友可以了解下2021-03-03
JDBC下Idea添加mysql-jar包的詳細(xì)過程
這篇文章主要介紹了JDBC下Idea添加mysql-jar包的詳細(xì)過程,添加jar包首先到官網(wǎng)下載jar包,然后idea導(dǎo)入jar包,在就是檢查,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11

