Skip to content
mermaid
sequenceDiagram
    participant Main as main()
    participant SPB as SpringApplication.run()
    participant SE as StandardEnvironment
    participant SCF as SpringApplicationConfiguration
    participant AC as ApplicationContext
    participant BeanFactory
    participant Events as 事件发布

    Main->>SPB: new SpringApplication(mainClass).run(args)
    
    Note over SPB,Events: 第一阶段:准备环境
    
    SPB->>SE: createEnvironment()
    SE-->>SPB: ConfigurableEnvironment
    
    SPB->>SCF: 创建 ApplicationContext 工厂
    
    Note over SPB,Events: 第二阶段:配置上下文
    
    SPB->>AC: prepareContext(environment)
    AC-->>SPB: ConfigurableApplicationContext
    
    Note over SPB,Events: 第三阶段:准备应用
    
    SPB->>AC: prepareContext(context, applicationArguments)
    SPB->>AC: postProcessApplicationContext()
    
    Note over SPB,Events: 第四阶段:刷新上下文(核心)
    
    SPB->>AC: context.refresh()
    
    rect rgba(0,100,200,0.2)
        AC->>BeanFactory: obtainFreshBeanFactory()
        AC->>BeanFactory: prepareBeanFactory(beanFactory)
        AC->>BeanFactory: postProcessBeanFactory()
        AC->>BeanFactory: invokeBeanFactoryPostProcessors()
        AC->>BeanFactory: registerListeners()
        AC->>BeanFactory: instantiateSingletons()
        AC->>BeanFactory: finishBeanFactoryInitialization()
        AC->>BeanFactory: finishRefresh()
    end
    
    Note over SPB,Events: 第五阶段:收尾工作
    
    AC->>Events: publishEvent(RefreshedEvent)
    SPB->>AC: callRunners(context, args)
    SPB-->>Main: return the running application

更新于:

note