Spring注解06 @Bean生命周期
bean的生命周期:bean创建---初始化----销毁的过程
容器管理bean的生命周期: 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
spring容器对管理bean的生命周期的四种方式:
1)、指定初始化和销毁方法;通过@Bean指定init-method和destroy-method;
2)、通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义销毁逻辑);
3)、可以使用JSR250;@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法@PreDestroy:在容器销毁bean之前通知我们进行清理工作
4)、BeanPostProcessor【interface】:bean的后置处理器;在bean初始化前后进行一些处理工作;postProcessBeforeInitialization:在初始化之前工作postProcessAfterInitialization:在初始化之后工作;
***Spring底层对 BeanPostProcessor 的使用:
bean赋值,注入其他组件,@Autowired,生命周期注解功能,@Async,xxx 都是通过BeanPostProcessor实现。
直接鲁代码:
- 配置类
@ComponentScan("com.tommy.bean")
@Configuration
public class MainConfigBeanLifeCircle {
//1)、指定初始化和销毁方法;通过@Bean指定init-method和destroy-method;
@Bean(initMethod = "init",destroyMethod = "destory")
//@Scope("prototype")
public Car car(){
return new Car();
}
}
- 测试类
public class IOCTestBeanLifeCircle {
@Test
public void testImport() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigBeanLifeCircle.class);
System.out.println("IOC容器创建完成...");
applicationContext.close();
}
}
- 第一种实现方式:指定初始化和销毁方法;通过@Bean指定init-method和destroy-method
public class Car {
public Car() {
System.out.println("car constrctor ...");
}
public void init() {
System.out.println("car init ...");
}
public void destory() {
System.out.println("car destory ...");
}
}
- 第二种实现方式:通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义销毁逻辑)
@Component
public class Dog implements InitializingBean, DisposableBean {
public Dog() {
System.out.println("dog ... constructor...");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("dog ... afterPropertiesSet...");
}
@Override
public void destroy() throws Exception {
System.out.println("dog ... destroy...");
}
}
- 第三种实现方式:可以使用JSR250;@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
@Component
public class Fish {
public Fish() {
System.out.println("Fish constructor ...");
}
@PostConstruct
public void init() {
System.out.println("Fish PostConstruct ...");
}
@PreDestroy
public void destory() {
System.out.println("Fish PreDestroy ...");
}
}
- 第四种方式:BBeanPostProcessor【interface】:bean的后置处理器;在bean初始化前后进行一些处理工作:postProcessBeforeInitialization:在初始化之前工作;postProcessAfterInitialization:在初始化之后工作;
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization---->" + " beanName = [" + beanName + "]");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization---->" + " beanName = [" + beanName + "]");
return bean;
}
}
- 测试结果:
postProcessBeforeInitialization----> beanName = [mainConfigBeanLifeCircle]
postProcessAfterInitialization----> beanName = [mainConfigBeanLifeCircle]
dog ... constructor...
postProcessBeforeInitialization----> beanName = [dog]
dog ... afterPropertiesSet...
postProcessAfterInitialization----> beanName = [dog]
Fish constructor ...
postProcessBeforeInitialization----> beanName = [fish]
Fish PostConstruct ...
postProcessAfterInitialization----> beanName = [fish]
car constrctor ...
postProcessBeforeInitialization----> beanName = [car]
car init ...
postProcessAfterInitialization----> beanName = [car]
IOC容器创建完成...
car destory ...
Fish PreDestroy ...
dog ... destroy...
Process finished with exit code 0
- 测试结果分析:
1)构造(对象创建)
单实例:在容器启动的时候创建对象
多实例:在每次获取的时候创建对象
BeanPostProcessor.postProcessBeforeInitialization
2)初始化:
对象创建完成,并赋值好,调用初始化方法。。。
BeanPostProcessor.postProcessAfterInitialization
3)销毁:
单实例:容器关闭的时候
多实例:容器不会管理这个bean;容器不会调用销毁方法;
- 代码原理:
遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,
一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
BeanPostProcessor原理
populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
- 代码调用
initializeBean
{
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}