Spring Bean的生命周期主要指的是单例bean(singleton),对于多例bean,Spring在创建好后就不会管理后续的生命周期。
对于普通的Java对象,它们的生命周期一般就是:
- 实例化
- 不再使用时,被垃圾回收器回收
而对于Spring bean来说,大致上它的生命周期:
实例化–>属性赋值–>初始化–>销毁
1、实例化
通过 Java 的反射机制或工厂方法创建一个 Bean 的实例
2、属性赋值
构造函数或者setter方法
3、初始化和销毁方法
以下主要讲初始化和销毁的方法:
方式一: 自定义init-method和destroy-method
具体操作:
public class Car {
public Car(){
System.out.println("Car ... Construct.....");
}
public void init(){
System.out.println("Car ... init......");
}
public void destroy(){
System.out.println("Car ... destroy.......");
}
}
配置类:
@Configuration
@ComponentScan(value = {"com.herd.beanlifecycle"})
public class MyConfig3 {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Car car(){
return new Car();
}
}
测试方法:
@Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("容器创建完成....");
context.close();
}
方式二:让Bean实现
InitializingBean
和DisposableBean
接口,并重写方法
具体操作:
public class Cat implements InitializingBean, DisposableBean {
public Cat(){
System.out.println("Cat ... construct......");
}
/**
* 初始化方法
*/
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Cat ... init .....");
}
/**
* 销毁方法
*/
@Override
public void destroy() throws Exception {
System.out.println("Cat ... destroy......");
}
}
配置类:
@Configuration
public class MyConfig3 {
/**
*实现 InitializingBean, DisposableBean接
*/
@Bean
public Cat cat(){
return new Cat();
}
}
测试类:
@Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("容器创建完成....");
context.close();
}
测试结果:
第三种方式:使用JSR250规范的两个注解
具体操作:
public class Dog {
public Dog(){
System.out.println("Dog ... Construct.....");
}
@PostConstruct
public void init(){
System.out.println("Dog ... init......");
}
@PreDestroy
public void destroy(){
System.out.println("Dog ... destroy.......");
}
}
配置类:
@Configuration
public class MyConfig3 {
/**
* 使用JSR250规定的方法
*/
/**
* @PostConstruct: 在Bean创建完成并属性赋值完成,来执行初始化方法
* @PreDestroy: 在容器销毁之前,通知清理工作
*/
@Bean
public Dog dog(){
return new Dog();
}
}
测试类:
@Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("容器创建完成....");
context.close();
}
测试结果:
在初始化方法之前我们也可以做一些操作,这就是后置处理器:实现BeanPostProcessor
接口,重写里面的两个方法
postProcessBeforeInitialization()
后置处理器 前置操作、postProcessAfterInitialization()
后置处理器 后置操作,Spring AOP的实现就是在后置处理器 后置方法 这里实现
具体实现:
编写一个类,并注入到容器中,以便观察后置处理器的执行时机
public class Car {
public Car(){
System.out.println("Car ... Construct.....");
}
public void init(){
System.out.println("Car ... init......");
}
public void destroy(){
System.out.println("Car ... destroy.......");
}
}
编写后置处理器类,实现BeanPostProcessor
接口,重写方法
@Component //交给容器管理
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* 后置处理器 前置操作
* @param bean the new bean instance 新创建的实例
* @param beanName the name of the bean 实例的名称
* @return
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization"+bean+"===>"+beanName);
return bean;
}
/**
* 后置处理器 后置操作 AOP
* @param bean the new bean instance
* @param beanName the name of the bean
* @return
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization"+bean+"===>"+beanName);
return bean;
}
}
配置类:
@Configuration
@ComponentScan(value = {"com.herd.beanlifecycle"})
public class MyConfig3 {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Car car(){
return new Car();
}
}
测试:
@Test
public void test01(){
//启动容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("容器创建完成....");
//关闭容器
context.close();
}
这里我们跟一下源码:
关键位置:
进入前置操作方法:
里面有很多的PostProcessor
循环找自己写的PostProcessor
然后跳出前置操作,进入初始化方法,在下图我们可以看出,执行了init方法
后置方法不再赘述。
总之,Spring Bean的生命周期: