spring bean的生命周期
一、bean的生命周期:
指的是bean的创建、初始化、销毁的过程
容器管理bean的生命周期
我们可以自定义初始化和销毁方法,容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
二、对象创建、初始化、销毁的时机:
对象创建:
- 单实例:在容器启动的时候创建对象
- 多实例:在每次获取的时候创建对象
初始化:
- 对象创建完成,并赋值好,调用初始化方法。。。
销毁:
- 单实例:容器关闭的时候
- 多实例:容器不会管理这个bean;容器不会调用销毁方法;
三、自定义bean的几种方式:
1、指定初始化和销毁方法:
通过@Bean指定initMethod和destroyMethod
@Configuration
public class MainConfigOfLifeCycle {
@Bean(initMethod = "init", destroyMethod = "destory")
public Car car() {
return new Car();
}
}
public class Car {
public Car() {
System.out.println("car constructor");
}
public void init() {
System.out.println("car...init...");
}
public void destory() {
System.out.println("car..destory...");
}
}
2、通过让Bean实现InitializingBean(定义初始化逻辑)、DisposableBean(定义销毁逻辑)
afterPropertiesSet():当beanFacoty创建好对象并且已经把bean的属性设置完成后调用
@Component public class Cat implements InitializingBean, DisposableBean { public Cat() { System.out.println("Cat Constructor"); } public void afterPropertiesSet() throws Exception { System.out.println("Cat...afterPropertiesSet"); } public void destroy() throws Exception { System.out.println("Cat destroy"); } }
3、使用JSR250;
@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
@PreDestroy:在容器销毁bean之前通知我们进行清理工作
@Component public class Dog implements ApplicationContextAware { @Autowired private ApplicationContext applicationContext; 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.."); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
4、BeanPostProcessor:bean的后置处理器, 在bean初始化前后进行一些处理工作;
postProcessBeforeInitialization:在初始化之前工作
postProcessAfterInitialization:在初始化之后工作
@Component public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization..." + beanName); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization " + beanName); return bean; } }
代码执行结果:
BeanPostProcessor postProcessBeforeInitialization...org.springframework.context.event.internalEventListenerProcessor BeanPostProcessor postProcessAfterInitialization org.springframework.context.event.internalEventListenerProcessor BeanPostProcessor postProcessBeforeInitialization...org.springframework.context.event.internalEventListenerFactory BeanPostProcessor postProcessAfterInitialization org.springframework.context.event.internalEventListenerFactory BeanPostProcessor postProcessBeforeInitialization...mainConfigOfLifeCycle BeanPostProcessor postProcessAfterInitialization mainConfigOfLifeCycle car constructor BeanPostProcessor postProcessBeforeInitialization...car car...init...initMethod BeanPostProcessor postProcessAfterInitialization car Boss...有参构造器 BeanPostProcessor postProcessBeforeInitialization...boss BeanPostProcessor postProcessAfterInitialization boss Cat Constructor BeanPostProcessor postProcessBeforeInitialization...cat Cat...afterPropertiesSet InitializingBean BeanPostProcessor postProcessAfterInitialization cat dog...constructor.. 调用ApplicationContextAware.setApplicationContext() BeanPostProcessor postProcessBeforeInitialization...dog dog..@PostConstruct.. BeanPostProcessor postProcessAfterInitialization dog container created finished... Dog...@PreDestroy.. Cat destroy DisposableBean car..destory...destroyMethod