bean的生命周期:
bean创建---初始化----销毁的过程
容器管理bean的生命周期;
我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
构造(对象创建)
单实例:在容器启动的时候创建对象
多实例:在每次获取的时候创建对象
BeanPostProcessor.postProcessBeforeInitialization(初始化之前)
初始化:
对象创建完成,并赋值好,调用初始化方法。。。
BeanPostProcessor.postProcessAfterInitialization(初始化之后)
销毁:
单实例:容器关闭的时候
多实例:容器不会管理这个bean;容器不会调用销毁方法;
1.指定初始化和销毁方法;
通过@Bean指定init-method和destroy-method;
- 创建一个bean
package com.spring.annotation.bean;
public class Car {
public Car() {
System.out.println("car constructor.....");
}
public void init() {
System.out.println("car ... init ...");
}
public void destroy() {
System.out.println("car...destroy .....");
}
}
- @Bean(initMethod = "init", destroyMethod = "destroy") 指定初始化和销毁方法
@Configuration
public class MainConfigOfLifeCycle {
@Bean(initMethod = "init", destroyMethod = "destroy")
public Car car(){
return new Car();
}
}
2.通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义销毁逻辑);
@Component
public class Cat implements InitializingBean, DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("Cat destroy....");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("cat... afterPropertiesSet...");
}
}
- 可以使用JSR250;@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
@PreDestroy:在容器销毁bean之前通知我们进行清理工作
@Component
public class Dog {
public Dog() {
System.out.println("Dog constructor....");
}
/**
* 对象创建并赋值之后调用
*/
@PostConstruct
public void init(){
System.out.println("Dog.....@PostConstruct");
}
/**
* 容器移除对象之前调用
*/
@PreDestroy
public void destory(){
System.out.println("Dog.....@PreDestroy");
}
}
4.BeanPostProcessor【interface】:bean的后置处理器;在bean初始化前后进行一些处理工作;postProcessBeforeInitialization:在初始化之前工作
postProcessAfterInitialization:在初始化之后工作
/**
* bean 后置处理器 初始化前后进行处理工作
* 可以返回 当前传递参数的 bean实例 也可以返回一个包装好的bean
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
}
- 测试 以上四种 指定bean生命周期的方法(注意添加 包扫描)
/**
* bean的生命周期
*
* bean的创建----初始化-----销毁的过程
*
* 容器管理bean的生命周期
* 我们可以自定义初始化和销毁方法
* 容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
*/
@ComponentScan("com.spring.annotation.bean")
@Configuration
public class MainConfigOfLifeCycle {
@Bean(initMethod = "init", destroyMethod = "destroy")
public Car car(){
return new Car();
}
}
- 单元测试
@Test
public void testBeanLifeCycle() {
//创建IOC容器
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("容器创建完成");
//容器关闭的时候调用 销毁方法
applicationContext.close();
}
-测试结果
postProcessBeforeInitialization...org.springframework.context.event.internalEventListenerProcessor=>org.springframework.context.event.EventListenerMethodProcessor@55040f2f
postProcessAfterInitialization...org.springframework.context.event.internalEventListenerProcessor=>org.springframework.context.event.EventListenerMethodProcessor@55040f2f
postProcessBeforeInitialization...org.springframework.context.event.internalEventListenerFactory=>org.springframework.context.event.DefaultEventListenerFactory@400cff1a
postProcessAfterInitialization...org.springframework.context.event.internalEventListenerFactory=>org.springframework.context.event.DefaultEventListenerFactory@400cff1a
postProcessBeforeInitialization...mainConfigOfLifeCycle=>com.spring.annotation.config.MainConfigOfLifeCycle$$EnhancerBySpringCGLIB$$f57ce7ce@525f1e4e
postProcessAfterInitialization...mainConfigOfLifeCycle=>com.spring.annotation.config.MainConfigOfLifeCycle$$EnhancerBySpringCGLIB$$f57ce7ce@525f1e4e
postProcessBeforeInitialization...cat=>com.spring.annotation.bean.Cat@4de5031f
cat... afterPropertiesSet...
postProcessAfterInitialization...cat=>com.spring.annotation.bean.Cat@4de5031f
Dog constructor....
postProcessBeforeInitialization...dog=>com.spring.annotation.bean.Dog@29f69090
Dog.....@PostConstruct
postProcessAfterInitialization...dog=>com.spring.annotation.bean.Dog@29f69090
car constructor.....
postProcessBeforeInitialization...car=>com.spring.annotation.bean.Car@691a7f8f
car ... init ...
postProcessAfterInitialization...car=>com.spring.annotation.bean.Car@691a7f8f
三月 26, 2019 1:04:38 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@532760d8: startup date [Tue Mar 26 01:04:38 CST 2019]; root of context hierarchy
容器创建完成
car...destroy .....
Dog.....@PreDestroy
Cat destroy....
Process finished with exit code 0
注意观察 bean 初始化 执行顺序
postProcessBeforeInitialization...car=>com.spring.annotation.bean.Car@691a7f8f
car ... init ...
postProcessAfterInitialization...car=>com.spring.annotation.bean.Car@691a7f8f
初始化之前 postProcessBeforeInitialization
初始化之后 postProcessAfterInitialization