在Spring框架中,Bean的生命周期是指从Spring容器实例化Bean对象,到管理其整个生命周期的过程。了解Bean的生命周期对于开发者来说非常重要,因为它有助于正确地管理资源、初始化Bean属性以及处理销毁时的清理工作。下面是Spring中Bean的生命周期的详细过程:
1. Bean实例化
Spring容器通过调用Bean的无参构造方法或静态工厂方法来创建Bean实例。
2. 设置Bean属性
Spring容器根据配置文件中的信息,使用依赖注入(DI,Dependency Injection)设置Bean的属性。此时,Spring会调用setter方法或通过构造函数注入属性。
3. BeanNameAware 接口
如果Bean实现了BeanNameAware接口,Spring会调用该Bean的setBeanName方法,传递Bean的名字。
4. BeanFactoryAware 接口
如果Bean实现了BeanFactoryAware接口,Spring会调用该Bean的setBeanFactory方法,传递BeanFactory实例。
5. ApplicationContextAware 接口
如果Bean实现了ApplicationContextAware接口,Spring会调用该Bean的setApplicationContext方法,传递ApplicationContext实例。
6. Post-Processor(后置处理器)
Spring容器在初始化Bean之前和之后会调用Bean后置处理器(BeanPostProcessor)。
- postProcessBeforeInitialization(Object bean, String beanName):在Bean初始化之前调用。
- postProcessAfterInitialization(Object bean, String beanName):在Bean初始化之后调用。
7. InitializingBean 接口
如果Bean实现了InitializingBean接口,Spring会调用其afterPropertiesSet方法。这个方法通常用于初始化Bean。
8. 自定义初始化方法
如果在Spring配置文件或注解中指定了初始化方法(通过init-method属性或@PostConstruct注解),Spring会调用这个方法。这个方法在afterPropertiesSet之后调用。
9. Bean就绪使用
此时,Bean已经完全初始化并准备好被应用程序使用。
10. 容器关闭
当Spring容器关闭时,会开始销毁Bean的过程。
11. DisposableBean 接口
如果Bean实现了DisposableBean接口,Spring会调用其destroy方法。这个方法通常用于释放资源。
12. 自定义销毁方法
如果在Spring配置文件或注解中指定了销毁方法(通过destroy-method属性或@PreDestroy注解),Spring会调用这个方法。这个方法在destroy之后调用。
Bean生命周期的完整过程图
plaintext
+-------------------+
| Instantiate Bean |
+---------+---------+
|
v
+-------------------+
| Populate Bean |
| Properties |
+---------+---------+
|
v
+-------------------+
| BeanNameAware |
| setBeanName(name) |
+---------+---------+
|
v
+-------------------+
| BeanFactoryAware |
| setBeanFactory() |
+---------+---------+
|
v
+-------------------+
| ApplicationContext|
| Aware |
| setApplicationCtx()|
+---------+---------+
|
v
+-------------------+
| BeanPostProcessor|
| beforeInit(bean) |
+---------+---------+
|
v
+-------------------+
| InitializingBean |
| afterPropertiesSet|
+---------+---------+
|
v
+-------------------+
| Custom Init |
| Method |
+---------+---------+
|
v
+-------------------+
| BeanPostProcessor|
| afterInit(bean) |
+---------+---------+
|
v
+-------------------+
| Bean Ready for |
| Use |
+---------+---------+
|
v
+-------------------+
| Container |
| Shutdown |
+---------+---------+
|
v
+-------------------+
| DisposableBean |
| destroy() |
+---------+---------+
|
v
+-------------------+
| Custom Destroy |
| Method |
+-------------------+
示例代码
定义一个Bean
java
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class MyBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String name;
public MyBean() {
System.out.println("Constructor called: MyBean");
}
public void setName(String name) {
this.name = name;
System.out.println("Property set: name = " + name);
}
@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware: setBeanName called with " + name);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
System.out.println("BeanFactoryAware: setBeanFactory called");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
System.out.println("ApplicationContextAware: setApplicationContext called");
}
@Override
public void afterPropertiesSet() {
System.out.println("InitializingBean: afterPropertiesSet called");
}
@PostConstruct
public void customInit() {
System.out.println("Custom init-method: @PostConstruct called");
}
@PreDestroy
public void customDestroy() {
System.out.println("Custom destroy-method: @PreDestroy called");
}
@Override
public void destroy() {
System.out.println("DisposableBean: destroy called");
}
}
Spring配置类
java
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyBean myBean = context.getBean(MyBean.class);
myBean.setName("Test Bean");
context.close();
}
}
通过上述示例,可以完整地看到Spring中Bean生命周期的各个阶段的执行情况。
07-01
1681
07-18
11万+
02-26
14万+
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交