Spring Bean 的生命周期是指 Spring 容器创建、初始化、使用和销毁 Bean 的全过程。这个生命周期包括多个阶段,下面我将详细解释这些阶段:
Spring Bean 的生命周期
-
实例化(Instantiation):
- Spring 容器根据配置文件或注解实例化 Bean 对象。
- 通过反射机制创建 Bean 的实例。
-
属性注入(Dependency Injection):
- Spring 容器将 Bean 的依赖属性注入到 Bean 实例中。
- 可以通过构造函数注入或 Setter 方法注入。
-
BeanNameAware 接口的 setBeanName 方法:
- 如果 Bean 实现了
BeanNameAware
接口,Spring 容器将调用该接口的setBeanName
方法,传递 Bean 的名称。
- 如果 Bean 实现了
-
BeanFactoryAware 接口的 setBeanFactory 方法:
- 如果 Bean 实现了
BeanFactoryAware
接口,Spring 容器将调用该接口的setBeanFactory
方法,传递BeanFactory
实例。
- 如果 Bean 实现了
-
ApplicationContextAware 接口的 setApplicationContext 方法:
- 如果 Bean 实现了
ApplicationContextAware
接口,Spring 容器将调用该接口的setApplicationContext
方法,传递ApplicationContext
实例。
- 如果 Bean 实现了
-
BeanPostProcessor 的 preProcessBeforeInitialization 方法:
- 如果有
BeanPostProcessor
实现类,Spring 容器将在初始化之前调用postProcessBeforeInitialization
方法,对 Bean 进行处理。
- 如果有
-
InitializingBean 接口的 afterPropertiesSet 方法:
- 如果 Bean 实现了
InitializingBean
接口,Spring 容器将调用该接口的afterPropertiesSet
方法进行初始化。
- 如果 Bean 实现了
-
自定义初始化方法:
- 如果在配置文件中指定了初始化方法,Spring 容器将调用这个方法。
-
BeanPostProcessor 的 postProcessAfterInitialization 方法:
- 如果有
BeanPostProcessor
实现类,Spring 容器将在初始化之后调用postProcessAfterInitialization
方法,对 Bean 进行处理。
- 如果有
-
Bean 的使用:
- Bean 已经完全初始化,可以被应用程序使用。
-
DisposableBean 接口的 destroy 方法:
- 如果 Bean 实现了
DisposableBean
接口,Spring 容器将调用该接口的destroy
方法进行销毁操作。
- 如果 Bean 实现了
-
自定义销毁方法:
- 如果在配置文件中指定了销毁方法,Spring 容器将调用这个方法。
示例代码
下面是一个完整的示例代码,展示了一个 Bean 的生命周期中涉及的各个接口和方法:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String property;
public void setProperty(String property) {
this.property = property;
System.out.println("Property set: " + property);
}
@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware: " + name);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware: " + beanFactory);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware: " + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean: afterPropertiesSet");
}
public void init() {
System.out.println("Custom init method");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean: destroy");
}
public void cleanup() {
System.out.println("Custom destroy method");
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
((ClassPathXmlApplicationContext) context).close();
}
}
beans.xml 配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myBean" class="com.example.MyBean" init-method="init" destroy-method="cleanup">
<property name="property" value="value" />
</bean>
</beans>
总结
- 实例化:创建 Bean 实例。
- 属性注入:注入依赖属性。
- Aware 接口:设置 Bean 名称、Bean 工厂、应用上下文。
- 初始化:调用
BeanPostProcessor
、InitializingBean
、自定义初始化方法。 - 使用:Bean 可被应用程序使用。
- 销毁:调用
DisposableBean
、自定义销毁方法。