Spring的生命週期(三)

bean的生命週期: bean創建—初始化----銷燬的過程

容器管理bean的生命週期:我們可以自定義初始化和銷燬方法;容器在bean進行到當前生命週期的時候來調用我們自定義的初始化和銷燬方法.

構造對象創建

  •  單實例:在容器啓動的時候創建對象.
    
  •  多實例:在每次獲取的時候創建對象.
    

初始化方法:對象創建完成,並賦值好,調用初始化方法.

銷燬

  •  單實例:容器關閉的時候
    
  •  多實例:容器不會管理這個bean;容器不會調用銷燬方法;
    

通過@Bean指定init-method和destroy-method指定初始化和銷燬方法;

下面舉個例子說明一下:

  • 創建一個car實例對象:
@Component
public class Car {
	//Car的構造方法
	public Car(){
		System.out.println("car constructor...");
	}
	//初始化方法(需在@Bean中定義)
	public void init(){
		System.out.println("car ... init...");
	}
	//銷燬方法(需在@Bean中定義)
	public void detory(){
		System.out.println("car ... detory...");
	}

}

創建Configuration對象:

@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
	//@Scope("prototype")
	@Bean(initMethod="init",destroyMethod="detory")
	public Car car(){
		return new Car();
	}
}

寫一個測試類:

public class IOCTest_LifeCycle {
	
	@Test
	public void test01(){
		//1、創建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器創建完成...");
		//關閉容器
		applicationContext.close();
	}

}

打印結果如下:
在這裏插入圖片描述
注: 當使用@Scope(“prototype”) 直接定義Bean時,需使用applicationContext.getBean(“car”)方法才能獲取Bean.容器不會管理這個bean;不會調用銷燬方法.打印結果如下:(並不會調用銷燬方法)
在這裏插入圖片描述

  • 使用InitializingBean,DisposableBean實現對象的初始化和銷燬方法.
@Component
public class Cat implements InitializingBean,DisposableBean {
	
	public Cat(){
		System.out.println("cat constructor...");
	}

	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...destroy...");
	}

	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...afterPropertiesSet...");
	}

}

測試一下:

	@Test
	public void test01(){
		//1、創建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器創建完成...");

		applicationContext.close();
	}

打印結果如下:
在這裏插入圖片描述

  • 使用可以使用JSR250,@PostConstruct,@PreDestroy
@Component
public class Dog{
	public Dog(){
		System.out.println("dog constructor...");
	}
	
	//對象創建並賦值之後調用
	@PostConstruct
	public void init(){
		System.out.println("Dog....@PostConstruct...");
	}
	
	//容器移除對象之前
	@PreDestroy
	public void detory(){
		System.out.println("Dog....@PreDestroy...");
	}
}

執行測試類,打印如下:
在這裏插入圖片描述

  • BeanPostProcessor【interface】:bean的後置處理器
    在bean初始化前後進行一些處理工作:
  •  postProcessBeforeInitialization:在初始化之前工作
    
  •  postProcessAfterInitialization:在初始化之後工作
    

定義MyBeanPostProcessor

@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;
	}
}

運行測試類,打印結果如下:

postProcessBeforeInitialization…org.springframework.context.event.internalEventListenerProcessor=>org.springframework.context.event.EventListenerMethodProcessor@5b0abc94
postProcessAfterInitialization…org.springframework.context.event.internalEventListenerProcessor=>org.springframework.context.event.EventListenerMethodProcessor@5b0abc94
postProcessBeforeInitialization…org.springframework.context.event.internalEventListenerFactory=>org.springframework.context.event.DefaultEventListenerFactory@1f1c7bf6
postProcessAfterInitialization…org.springframework.context.event.internalEventListenerFactory=>org.springframework.context.event.DefaultEventListenerFactory@1f1c7bf6
postProcessBeforeInitialization…mainConfigOfLifeCycle=>com.atguigu.config.MainConfigOfLifeCycleEnhancerBySpringCGLIB16959f53@2b546384
postProcessAfterInitialization…mainConfigOfLifeCycle=>com.atguigu.config.MainConfigOfLifeCycleEnhancerBySpringCGLIB16959f53@2b546384
car constructor…
postProcessBeforeInitialization…car=>com.atguigu.bean.Car@b62fe6d
car … init…
postProcessAfterInitialization…car=>com.atguigu.bean.Car@b62fe6d
Boss…有參構造器
postProcessBeforeInitialization…boss=>Boss [car=com.atguigu.bean.Car@b62fe6d]
postProcessAfterInitialization…boss=>Boss [car=com.atguigu.bean.Car@b62fe6d]
cat constructor…
postProcessBeforeInitialization…cat=>com.atguigu.bean.Cat@7c417213
cat…afterPropertiesSet…
postProcessAfterInitialization…cat=>com.atguigu.bean.Cat@7c417213
dog constructor…
postProcessBeforeInitialization…dog=>com.atguigu.bean.Dog@3745e5c6
Dog…@PostConstruct…
postProcessAfterInitialization…dog=>com.atguigu.bean.Dog@3745e5c6
Dog…@PreDestroy…
cat…destroy…
car … detory…

BeanPostProcessor原理:

populateBean(beanName, mbd, instanceWrapper);給bean進行屬性賦值
initializeBean
{
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);執行自定義初始化
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章