在 Spring Boot 应用中,Bean 是构成应用程序的基础组件。理解 Bean 的生命周期对于开发高效、稳定的 Spring Boot 应用至关重要。本文将深入探讨 Spring Boot 中 Bean 的完整生命周期过程。
Bean 生命周期指的是从 Bean 的定义、创建、初始化、使用到销毁的整个过程。Spring 容器负责管理这些阶段,开发者可以通过各种方式干预这些过程。
@Component
、@Service
、@Repository
、@Controller
等注解定义@Configuration
) 定义容器通过以下方式创建 Bean 实例:
// 构造器实例化
@Component
public class MyBean {
public MyBean() {
System.out.println("1. 构造器执行 - 实例化阶段");
}
}
@Autowired
、@Value
等注解注入依赖@Component
public class MyBean {
@Value("${my.property}")
private String property;
@Autowired
private AnotherBean anotherBean;
}
BeanNameAware
: 设置 Bean 的名称BeanFactoryAware
: 设置 BeanFactoryApplicationContextAware
: 设置 ApplicationContext@Component
public class MyBean implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println("2. BeanNameAware 回调 - beanName: " + name);
}
}
postProcessBeforeInitialization
方法@PostConstruct
注解的方法InitializingBean
接口的 afterPropertiesSet
方法init-method
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("3. @PostConstruct 初始化方法执行");
}
}
postProcessAfterInitialization
方法@AutoConfigureBefore
指定当前自动配置类应该在某些其他自动配置类之前加载。
package com.zh.cn.config;
import com.zh.cn.controller.CoreController;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @Description:
* @author: zh
* @Create : 2025/4/11
* @Project_name : data
* @Version :
**/
@Slf4j
@Component
@AutoConfigureBefore(CoreConfig.class)
public class TestAConfig {
public TestAConfig(){
log.info("testAConfig 初始化");
}
@PostConstruct
public void init(){
log.info("testAConfig init 初始化完成");
}
@PreDestroy
public void destory(){
log.info("testAConfig destory 销毁释放资源");
}
public String getName(){
return "TestAConfig";
}
}
@AutoConfigureAfter指定当前自动配置类应该在某些其他自动配置类之后加载
package com.zh.cn.config;
import com.zh.cn.controller.CoreController;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @Description:
* @author: zh
* @Create : 2025/4/11
* @Project_name : data
* @Version :
**/
@Slf4j
@Component
@AutoConfigureAfter(CoreConfig.class)
public class TestAConfig {
public TestAConfig(){
log.info("testAConfig 初始化");
}
@PostConstruct
public void init(){
log.info("testAConfig init 初始化完成");
}
@PreDestroy
public void destory(){
log.info("testAConfig destory 销毁释放资源");
}
public String getName(){
return "TestAConfig";
}
}
@PreDestroy
注解的方法DisposableBean
接口的 destroy
方法destroy-method
@Component
public class MyBean {
@PreDestroy
public void destroy() {
System.out.println("4. @PreDestroy 销毁方法执行");
}
}
Bean定义 → 实例化 → 属性赋值 → Aware接口回调 → BeanPostProcessor前置处理 →
初始化方法(@PostConstruct/afterPropertiesSet/init-method) →
BeanPostProcessor后置处理 → 使用阶段 →
销毁方法(@PreDestroy/destroy/destroy-method) → Bean销毁
@Component
public class CustomBean implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() {
// 初始化逻辑
}
@Override
public void destroy() {
// 销毁逻辑
}
}
通过order注解可以指定初始化完成后的执行顺序
@Slf4j
@AutoConfigureBefore(TestAConfig.class)
@Component
public class TestBConfig implements InitializingBean, DisposableBean {
public TestBConfig(TestAConfig testAConfig){
System.out.println(testAConfig.getName());
log.info("testBConfig 初始化");
}
@Override
@Order(1)
public void afterPropertiesSet() throws Exception {
log.info("TestBConfig init afterPropertiesSet");
}
@PostConstruct
@Order(-1)
public void init(){
log.info("TestBConfig init 2 ");
}
@Override
public void destroy() throws Exception {
log.info("TestBConfig destory");
}
}
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
// 前置处理逻辑
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
// 后置处理逻辑
return bean;
}
}
@DependsOn
注解指定依赖关系@Order
控制 BeanPostProcessor 执行顺序@Lazy
)Spring Boot 中 Bean 的生命周期是一个精心设计的过程,提供了多个扩展点供开发者干预。理解这些阶段和扩展点,能够帮助我们更好地控制组件的行为,构建更加健壮的应用。在实际开发中,应根据具体需求选择合适的干预方式,同时注意避免常见的陷阱和性能问题。
通过合理利用生命周期回调,我们可以实现更优雅的资源管理、更高效的性能优化和更可靠的错误处理,从而提升整个应用的质量和可维护性。