1.@Bean指定初始化和銷燬方法
BigAminal類,自定義了init和destory方法
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class BigAnimal {
public BigAnimal() {
System.out.println("BigAnimal->Construtor");
}
public void init(){
System.out.println("BigAnimal ->init");
}
public void destory(){
System.out.println("BigAnimal->destory");
}
}
配置類LifeCyle,使用@Bean註解的initMthod和destroyMethod指定初始化和銷燬方法
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
@Configuration
public class LifeCycleConfig {
@Bean(initMethod = "init",destroyMethod = "destory")
public BigAnimal animal(){
return new BigAnimal();
}
}
測試類LifeCycleConfigTest
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class LifeCycleConfigTest {
private AnnotationConfigApplicationContext annotationConfigApplicationContext =new AnnotationConfigApplicationContext(LifeCycleConfig.class);
@Test
public void animal() {
BigAnimal bigAnimal = annotationConfigApplicationContext.getBean(BigAnimal.class);
annotationConfigApplicationContext.close();
}
}
運行結果
2.實現 InitializingBean和DisposableBean接口
通過讓Bean實現InitializingBean(定義初始化邏輯),DisposableBean(定義銷燬邏輯)
Man實現了InitalizingBean和DisposableBean接口,重寫afterPropertiesSet和destroy方法
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class Man implements InitializingBean,DisposableBean {
public Man() {
System.out.println("construtor:你一個男人出生了。。。。");
}
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet:他的爸爸給了他¥100000元");
}
public void destroy() throws Exception {
System.out.println("destroy:他把他的¥100000元,給了他兒子");
}
}
LifeCylceConfig配置類中註冊Bean
@Bean
public Man man(){
return new Man();
}
測試方法1中加入
@Test
public void man() {
Man man = annotationConfigApplicationContext.getBean(Man.class);
annotationConfigApplicationContext.close();
}
運行結果
3.使用JSR250的@PostConstruct和@PreDestroy註解
- @PostConstruct 在bean創建完成並賦值完成後,進行的初始化操作
- @PreDestroy 在容器銷燬bean之前,通知進行銷燬操作
WoMan類
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class Woman {
public Woman() {
System.out.println("Woman.Construtor ->一個女人出生了。。。。。。");
}
/**
*
* @throws Exception
*/
@PostConstruct
public void postConstruct() throws Exception {
System.out.println("Woman.postConstruct->她的爸爸給了她¥100000元");
}
/**
*
* @throws Exception
*/
@PreDestroy
public void preDestroy() throws Exception {
System.out.println("Woman->preDestroy她把她的¥100000元,給了她兒子");
}
}
配置類
@Bean
public Woman woman(){
return new Woman();
}
在測試1的測試類中添加測試方法3
@Test
public void woman() {
Woman woman = annotationConfigApplicationContext.getBean(Woman.class);
annotationConfigApplicationContext.close();
}
運行結果
4.BeanPostProcessor:Bean的後置處理器
在bean初始化前後進行一些處理工作;
- postProcessBeforeInitialization:在初始化之前工作
- postProcessAfterInitialization:在初始化之後工作
自定義BeanPostProcessor,MyBeanPostProcessor
/**
* @author GaoYuzhe
* @date 2018/3/14.
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{
/**
* 初始化前置處理
* @param bean Bean實例本身
* @param beanName 容器中註冊的BeanId
* @return
* @throws BeansException
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if("woman".equals(beanName)){
System.out.println("前置處理");
}
return bean;
}
/**
* 初始化後置處理
* @param bean Bean實例本身
* @param beanName 容器中註冊的BeanId
* @return
* @throws BeansException
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if("woman".equals(beanName)){
System.out.println("後置置處理");
}
return bean;
}
}
在原LifeClyceConfig上加上@ComponentScan(“com.daxiong.condition”),來掃描
@Configuration
@ComponentScan("com.daxiong.condition")
運行測試方法3
5.總結
bean的生命週期:bean創建—初始化—-銷燬的過程
容器管理bean的生命週期:我們可以我們可以自定義初始化和銷燬方法;容器在bean進行到當前生命週期的時候來調用我們自定義的初始化和銷燬方法
具體步驟:
構造(對象創建):
- 單實例:在容器啓動的時候創建對象
- 多實例:在每次獲取的時候創建對象
BeanPostProcessor.postProcessBeforeInitialization
- 初始化:對象創建完成,並賦值好,調用初始化方法。。。
- BeanPostProcessor.postProcessAfterInitialization
- 銷燬:
- 單實例:容器關閉的時候
- 多實例:容器不會管理這個bean;容器不會調用銷燬方法
遍歷得到容器中所有的BeanPostProcessor;挨個執行beforeInitialization,
一但返回null,跳出for循環,不會執行後面的BeanPostProcessor.postProcessorsBeforeInitialization
BeanPostProcessor原理
僞代碼
//給bean進行屬性賦值 populateBean(beanName, mbd, instanceWrapper); initializeBean(); { //前置處理 applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); //執行自定義初始化 invokeInitMethods(beanName, wrappedBean, mbd); //後置處理 applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); }