This document details the comprehensive execution order of the Spring Boot application bean lifecycle, encompassing related methods at each stage.
Phase 1: Bootstrapping (JVM & Spring Boot Initialization)
public static void main(String[] args)
).SpringApplication.run()
initiates the application context creation. (Related Method: SpringApplication.run()
)
application.properties
/yml
files, and command-line arguments. Active and default profiles are determined. (Related Methods: ConfigurableEnvironment#setActiveProfiles()
, PropertySourcesPropertyResolver#getProperty()
)
AnnotationConfigServletWebServerApplicationContext
(web) or AnnotationConfigApplicationContext
(non-web). (Related Method: SpringApplication#determineWebApplicationType()
)
META-INF/spring.factories
). (Related Method: SpringFactoriesLoader#loadFactoryNames()
)
SpringApplicationRunListeners
are triggered, firing events like ApplicationStartingEvent
and ApplicationEnvironmentPreparedEvent
. (Related Methods: SpringApplicationRunListeners#starting()
, SpringApplicationRunListeners#environmentPrepared()
)
Phase 2: Context Initialization & Bean Lifecycle
ApplicationContext
is created, and beans are scanned using annotations like @ComponentScan
and @Configuration
. (Related Method: AnnotationConfigApplicationContext#register()
)
BeanDefinitionRegistry#registerBeanDefinition()
)
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation()
)
BeanNameAware
, BeanFactoryAware
, EnvironmentAware
) are processed. (Related Methods: BeanNameAware#setBeanName()
, BeanClassLoaderAware#setBeanClassLoader()
, BeanFactoryAware#setBeanFactory()
, EnvironmentAware#setEnvironment()
, EmbeddedValueResolverAware#setEmbeddedValueResolver()
, etc.)
@Conditional
or @Profile
are evaluated and conditionally created based on specified conditions or active profiles. (Related Methods: Condition#matches()
, ConfigurableEnvironment#getActiveProfiles()
)
BeanPostProcessor#postProcessBeforeInitialization()
methods are executed.@PostConstruct
, InitializingBean.afterPropertiesSet()
, or the init-method
attribute in @Bean
annotations. (Related Methods: InitializingBean#afterPropertiesSet()
, @PostConstruct
)
BeanPostProcessor#postProcessAfterInitialization()
methods are executed.Phase 3: Application Startup Completion
ApplicationContext
is refreshed, completing dependency injection. The ContextRefreshedEvent
is fired. (Related Method: AbstractApplicationContext#refresh()
)
ServletContextInitializer
and WebApplicationInitializer
are executed (for servlet-based apps). (Related Method: ConfigurableWebServerApplicationContext#start()
)
CommandLineRunner
or ApplicationRunner
are executed, performing post-startup tasks. (Related Methods: CommandLineRunner#run()
, ApplicationRunner#run()
)
ApplicationReadyEvent
is fired, signaling that the application is fully initialized and ready to handle requests. (Related Method: ApplicationListener#onApplicationEvent(ApplicationReadyEvent)
)
Phase 4: Bean Destruction & Application Shutdown
spring.lifecycle.timeout-per-shutdown-phase
. (Related Method: SpringApplication#setRegisterShutdownHook(true)
)
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction()
methods are executed.DisposableBean.destroy()
, @PreDestroy
methods, or the destroy-method
attribute in @Bean
annotations. (Related Methods: DisposableBean#destroy()
, @PreDestroy
)
ApplicationContext
closes, firing the ContextClosedEvent
. (Related Method: ConfigurableApplicationContext#close()
)
SpringApplication.exit()
can be used to set custom exit codes (using ExitCodeGenerator
). (Related Method: SpringApplication#exit()
)
Phase 5: Advanced Considerations
@Lazy
): Beans are created only when accessed. (Related Method: DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(false)
)
@Lazy
, setter injection, or @DependsOn
to manage circular dependencies. (Related Method: AbstractAutowireCapableBeanFactory#doResolveDependency()
)
FactoryBean#getObject()
)
HealthIndicator#health()
)
spring.main.lazy-initialization=true
and tune garbage collection.ApplicationListener
): Allows hooking into startup/shutdown events. (Related Method: ApplicationListener#onApplicationEvent()
)
Summary of Execution Order:
SpringApplication.run()
→ Auto-Configuration → Context CreationThis detailed breakdown provides a comprehensive understanding of the Spring Boot bean lifecycle and its execution order. Understanding this order is crucial for debugging, optimizing, and extending Spring Boot applications.
The above is the detailed content of spring-: spring-boot-application-bean-lifecycle-comprehensive-execution-order-with-related-methods. For more information, please follow other related articles on the PHP Chinese website!