Spring Bean 的生命周期涵盖了 Bean 从创建到销毁的整个过程,主要包括以下几个阶段:
-
实例化(Instantiation):
- Spring 容器会通过反射机制创建 Bean 的实例。这一步仅仅是对象的实例化,并没有进行任何属性的赋值或初始化。
-
属性赋值(Populate Properties):
- 实例化后,Spring 容器会进行依赖注入,为 Bean 的属性设置值。如果 Bean 之间存在依赖关系,Spring 会先创建并初始化这些依赖 Bean,然后再将它们注入到当前 Bean 中。
-
初始化前(Pre-initialization):
- Spring 容器会在 Bean 初始化之前调用任何注册的
BeanPostProcessor
的postProcessBeforeInitialization
方法。这一步允许开发者在 Bean 初始化之前对其进行额外的处理。
- Spring 容器会在 Bean 初始化之前调用任何注册的
-
初始化(Initialization):
- 如果 Bean 实现了
InitializingBean
接口,Spring 会调用其afterPropertiesSet
方法。 - 同时,如果在 Bean 配置中指定了初始化方法(通过
init-method
属性或@PostConstruct
注解),Spring 也会调用这些方法。
- 如果 Bean 实现了
-
初始化后(Post-initialization):
- 在初始化完成后,Spring 容器会调用任何注册的
BeanPostProcessor
的postProcessAfterInitialization
方法。这一步允许开发者在 Bean 初始化完成后对其进行额外的处理。
- 在初始化完成后,Spring 容器会调用任何注册的
-
使用阶段(Using the Bean):
- 在完成初始化后,Bean 处于就绪状态,可以被应用程序使用。这段时间内,Bean 可以执行其核心逻辑和功能。
-
销毁前(Pre-destroy):
- 当容器关闭时,Spring 会在销毁 Bean 之前调用任何注册的
DestructionAwareBeanPostProcessor
的postProcessBeforeDestruction
方法。这一步允许开发者在 Bean 销毁之前对其进行清理操作。
- 当容器关闭时,Spring 会在销毁 Bean 之前调用任何注册的
-
销毁(Destruction):
- 如果 Bean 实现了
DisposableBean
接口,Spring 会调用其destroy
方法。 - 同时,如果在 Bean 配置中指定了销毁方法(通过
destroy-method
属性或@PreDestroy
注解),Spring 也会调用这些方法。
- 如果 Bean 实现了
以下是一个详细的代码示例,展示了各个生命周期阶段的处理:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Configuration
public class BeanLifecycleConfig {
@Bean(initMethod = "customInit", destroyMethod = "customDestroy")
public MyBean myBean() {
return new MyBean();
}
@Bean
public BeanPostProcessor customBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MyBean) {
System.out.println("Before Initialization: " + beanName);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MyBean) {
System.out.println("After Initialization: " + beanName);
}
return bean;
}
};
}
public static class MyBean implements InitializingBean, DisposableBean {
public MyBean() {
System.out.println("Bean Instantiated");
}
@PostConstruct
public void postConstruct() {
System.out.println("PostConstruct Method");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean's afterPropertiesSet Method");
}
public void customInit() {
System.out.println("Custom Init Method");
}
@PreDestroy
public void preDestroy() {
System.out.println("PreDestroy Method");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean's destroy Method");
}
public void customDestroy() {
System.out.println("Custom Destroy Method");
}
}
}
在这个例子中,MyBean
类展示了 Spring Bean 的完整生命周期:
- 实例化:通过构造函数创建 Bean 实例。
- 属性赋值:注入属性(本例中未显式展示)。
- 初始化前:调用
BeanPostProcessor
的postProcessBeforeInitialization
方法。 - 初始化:调用
@PostConstruct
注解的方法,InitializingBean
的afterPropertiesSet
方法和自定义的init-method
。 - 初始化后:调用
BeanPostProcessor
的postProcessAfterInitialization
方法。 - 使用阶段:Bean 处于就绪状态,可以被使用。
- 销毁前:调用
DestructionAwareBeanPostProcessor
的postProcessBeforeDestruction
方法(本例中未显式展示)。 - 销毁:调用
@PreDestroy
注解的方法,DisposableBean
的destroy
方法和自定义的destroy-method
。