通過實例解析spring對象生命周期
配置時指定初始化及銷毀方法:

Bean中提供對應(yīng)的初始化及銷毀方法:
package com.atguigu.bean;
import org.springframework.stereotype.Component;
@Component
public class Car {
public Car(){
System.out.println("car constructor...");
}
public void init(){
System.out.println("car ... init...");
}
public void detory(){
System.out.println("car ... detory...");
}
}
2、生命周期-InitializingBean和DisposableBean
通過讓Bean實現(xiàn)InitializingBean(定義初始化邏輯),DisposableBean(定義銷毀邏輯);
package com.atguigu.bean;
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...");
}
public void destroy() throws Exception {
// TODO Auto-generated method stub
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
}
}
3、生命周期-@PostConstruct&@PreDestroy
可以使用JSR250:
@PostConstruct:在bean創(chuàng)建完成并且屬性賦值完成;來執(zhí)行初始化方法
@PreDestroy:在容器銷毀bean之前通知我們進行清理工作
package com.atguigu.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
@Component
public class Dog{
public Dog(){
System.out.println("dog constructor...");
}
//對象創(chuàng)建并賦值之后調(diào)用
@PostConstruct
public void init(){
System.out.println("Dog....@PostConstruct...");
}
//容器移除對象之前
@PreDestroy
public void detory(){
System.out.println("Dog....@PreDestroy...");
}
}
4、生命周期-BeanPostProcessor-后置處理器
BeanPostProcessor【interface】:bean的后置處理器:
在bean初始化前后進行一些處理工作;
postProcessBeforeInitialization:在初始化之前工作
postProcessAfterInitialization:在初始化之后工作
package com.atguigu.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
* 后置處理器:初始化前后進行處理工作
* 將后置處理器加入到容器中
* @author lfy
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
}
原理
5.1 調(diào)用鏈
1、AnnotationConfigApplicationContext --> AnnotationConfigApplicationContext(Class<?>... componentClasses)()方法的 --> refresh()方法;
2、AbstractApplicationContext類 --> refresh()方法的 --> finishBeanFactoryInitialization(beanFactory);方法
3、AbstractApplicationContext類 --> finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)方法的 --> beanFactory.preInstantiateSingletons();方法;
4、DefaultListableBeanFactory類 --> preInstantiateSingletons()方法的 --> getBean(beanName);方法;
5、AbstractBeanFactory類 --> getBean(String name)方法的 --> doGetBean(name, null, null, false);
6、AbstractBeanFactory類 --> doGetBean(final String name, @Nullable final Class<T> requiredType,@Nullable final Object[] args, boolean typeCheckOnly)方法的 --> createBean(beanName, mbd, args); L320
7、AbstractAutowireCapableBeanFactory類 --> createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)方法的 --> doCreateBean(beanName, mbdToUse, args);
8、AbstractAutowireCapableBeanFactory類 --> doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)方法。目標執(zhí)行方法


5.2目標執(zhí)行方法
如下圖所示:
1、applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);方法為對象初始化前的執(zhí)行操作;
2、exposedObject = initializeBean(beanName, exposedObject, mbd);方法為對象執(zhí)行初始化操作;
3、initializeBean(beanName, exposedObject, mbd); -->wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);方法為對象初始化后的操作。


5.3具體執(zhí)行
遍歷得到容器中所有的BeanPostProcessor;挨個執(zhí)行beforeInitialization,一但返回null,跳出for循環(huán),不會執(zhí)行后面的BeanPostProcessor.postProcessorsBeforeInitialization

6、生命周期-BeanPostProcessor在Spring底層的使用
例:
bean賦值,注入其他組件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;
所有實現(xiàn)了BeanPostProcessor接口的類或接口都有此功能。

/**
* 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)用銷毀方法;
*
*
* 遍歷得到容器中所有的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);
*}
*
*
*
* 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:在初始化之后工作
*
* Spring底層對 BeanPostProcessor 的使用;
* bean賦值,注入其他組件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;
*
* @author lfy
*
*/
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot數(shù)據(jù)庫密碼加密的配置方法
這篇文章主要給大家介紹了關(guān)于springboot數(shù)據(jù)庫密碼加密的配置方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級問題
這篇文章主要介紹了關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)
這篇文章主要介紹了shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08
SpringBoot整合Graylog做日志收集實現(xiàn)過程
這篇文章主要為大家介紹了SpringBoot整合Graylog做日志收集實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

