Skip to content

Spring 单例 Bean 初始化完整生命周期(带主要扩展点)

text
┌────────────────────────────────────────────────────────────┐
│        容器启动(创建 ApplicationContext)                 │
└────────────────────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 1. 调用 BeanFactoryPostProcessor               │
   │    (如:PropertySourcesPlaceholderConfigurer)│
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 2. 注册 BeanPostProcessor(实现类实例化)      │
   │    (如:AutowiredAnnotationBeanPostProcessor)│
   └───────────────────────────────────────────────┘


┌────────────────────────────────────────────────────────────┐
│       对每个单例 Bean 按以下步骤依次进行:                   │
└────────────────────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 3. 实例化 Bean(构造方法执行)                  │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 4. 执行 BeanPostProcessor.postProcessBeforeInit │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 5. 属性注入(依赖注入)                         │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 6. 调用 Aware 接口(BeanNameAware、BeanFactoryAware等)│
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 7. 调用 InitializingBean.afterPropertiesSet()  │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 8. 执行 @PostConstruct / init-method            │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 9. 执行 BeanPostProcessor.postProcessAfterInit  │
   └───────────────────────────────────────────────┘


(对其他 Bean 重复步骤 3~9,直到所有单例 Bean 初始化完成)


   ┌───────────────────────────────────────────────┐
   │ 10. 调用 SmartInitializingSingleton.afterSingletonsInstantiated()│
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 11. ApplicationContext 完成刷新                │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 12. 发布 ContextRefreshedEvent                 │
   │     (触发 ApplicationListener 监听器)         │
   └───────────────────────────────────────────────┘


   ┌───────────────────────────────────────────────┐
   │ 13. 容器可对外提供服务                          │
   └───────────────────────────────────────────────┘

📌 关键点解释

  1. BeanFactoryPostProcessor

    • 处理 BeanDefinition(还没实例化 Bean)
    • 典型用途:修改配置属性、替换 Bean 定义
  2. BeanPostProcessor

    • 分两次调用:
      • beforeInit(在 Bean 初始化方法之前)
      • afterInit(在 Bean 初始化方法之后)
    • 常用于 AOP 代理、注解扫描处理
  3. InitializingBean.afterPropertiesSet()

    • 当前 Bean 依赖注入完成后调用
    • 仅作用于自己
  4. SmartInitializingSingleton.afterSingletonsInstantiated()

    • 所有单例初始化完成后,全局调用一次
    • 常用于依赖所有 Bean 的全局初始化逻辑
  5. ApplicationListener

    • 监听容器事件,例如 ContextRefreshedEvent(容器完全可用时触发)

版权声明