Bean 生命周期
一、完整生命周期流程
┌─────────────────────────────────────────────────────────────────┐
│ Bean 完整生命周期 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. 实例化(Instantiation) │
│ └─ new UserService() ← 构造函数 │
│ │
│ 2. 属性填充(Populate) │
│ └─ @Autowired、setXxx() ← 依赖注入 │
│ │
│ 3. 初始化前(Initialization - Aware) │
│ ├─ BeanNameAware.setBeanName() │
│ ├─ BeanFactoryAware.setBeanFactory() │
│ └─ ApplicationContextAware.setApplicationContext() │
│ │
│ 4. 初始化(Initialization) │
│ ├─ @PostConstruct 标注的方法 │
│ ├─ InitializingBean.afterPropertiesSet() │
│ └─ @Bean(initMethod = "init") │
│ │
│ 5. BeanPostProcessor.postProcessAfterInitialization() │
│ │
│ ════════════════════════════════════════════════════════════════│
│ [ Bean 就绪 ] │
│ ════════════════════════════════════════════════════════════════│
│ │
│ 6. 销毁前(Destruction - Aware) │
│ ├─ @PreDestroy 标注的方法 │
│ ├─ DisposableBean.destroy() │
│ └─ @Bean(destroyMethod = "cleanup") │
│ │
│ 7. 销毁(Destruction) │
│ │
└─────────────────────────────────────────────────────────────────┘二、各阶段详解
Aware 接口
java
@Service
public class UserService implements BeanNameAware,
BeanFactoryAware,
ApplicationContextAware,
EnvironmentAware {
private String beanName;
private BeanFactory beanFactory;
private ApplicationContext applicationContext;
private Environment environment;
@Override
public void setBeanName(String name) {
this.beanName = name;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}初始化方法
java
@Service
public class UserService {
@PostConstruct
public void postConstruct() {
System.out.println("@PostConstruct 执行");
}
public void init() {
System.out.println("init-method 执行");
}
public void destroy() {
System.out.println("destroy-method 执行");
}
}
@Configuration
public class Config {
@Bean(initMethod = "init", destroyMethod = "destroy")
public UserService userService() {
return new UserService();
}
}InitializingBean / DisposableBean
java
@Service
public class UserService implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean.afterPropertiesSet()");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy()");
}
}三、执行顺序
┌─────────────────────────────────────────────────────────────────┐
│ 初始化顺序 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. @PostConstruct │
│ 2. InitializingBean.afterPropertiesSet() │
│ 3. @Bean(initMethod) │
│ │
│ 执行结果: │
│ @PostConstruct │
│ InitializingBean.afterPropertiesSet() │
│ init-method │
│ │
└─────────────────────────────────────────────────────────────────┘四、BeanPostProcessor
前后置处理
java
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
if (bean instanceof UserService) {
System.out.println("Before: " + beanName);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof UserService) {
System.out.println("After: " + beanName);
}
return bean;
}
}内置处理器
1. AutowiredAnnotationBeanPostProcessor → @Autowired
2. CommonAnnotationBeanPostProcessor → @PostConstruct, @PreDestroy
3. RequiredAnnotationBeanPostProcessor → @Required
4. AnnotationAwareAspectJAutoProxyCreator → AOP
5. ApplicationContextAwareProcessor → Aware 接口五、面试高频问题
Q1: 初始化和销毁的执行顺序?
@PostConstruct → InitializingBean.afterPropertiesSet() → @Bean(initMethod)
Q2: BeanPostProcessor 用途?
- 生成代理对象
- 处理注解(@Autowired、@PostConstruct)
- AOP 自动代理
Q3: 为什么构造器注入更推荐?
依赖在构造时已完成,保证 Bean 不被创建在不完整状态
六、下一章预告
下一章我们将学习 动态代理与 AOP 原理:
- JDK 动态代理
- CGLIB 字节码代理
- Spring AOP 原理