1️⃣ Bootstrapping Phase (JVM & Spring Boot Startup)
-
JVM starts and loads the
main
class (public static void main(String[] args)
). -
SpringApplication.run()
executes to initialize the application. 💡 Related method:SpringApplication.run()
-
Environment Setup:
- Loads system properties, environment variables,
application.properties/yml
, and command-line arguments. - Determines active and default profiles.
💡 Related method:
ConfigurableEnvironment#setActiveProfiles()
,PropertySourcesPropertyResolver#getProperty()
- Loads system properties, environment variables,
-
Application Type Determined:
-
Web App →
AnnotationConfigServletWebServerApplicationContext
-
Non-Web App →
AnnotationConfigApplicationContext
💡 Related method:SpringApplication#determineWebApplicationType()
-
Web App →
-
AutoConfiguration & SpringFactoriesLoader:
- Automatically registers dependencies from the classpath (
META-INF/spring.factories
). 💡 Related method:SpringFactoriesLoader#loadFactoryNames()
- Automatically registers dependencies from the classpath (
-
SpringApplicationRunListeners Triggered:
- Fires
ApplicationStartingEvent
,ApplicationEnvironmentPreparedEvent
. 💡 Related method:SpringApplicationRunListeners#starting()
,SpringApplicationRunListeners#environmentPrepared()
- Fires
2️⃣ Context Initialization & Bean Lifecycle
-
ApplicationContext is created and beans are scanned (
@ComponentScan
,@Configuration
). 💡 Related method:AnnotationConfigApplicationContext#register()
-
Bean Definition is Loaded (Metadata Processing):
- Spring reads bean definitions from configuration files (
@Configuration
), XML files, or component scanning (@Component
). - This is a metadata processing step—no instances are created yet.
💡 Related method:
BeanDefinitionRegistry#registerBeanDefinition()
- Spring reads bean definitions from configuration files (
-
Beans are instantiated (Object Creation):
- Constructor-based instantiation.
- Factory method instantiation.
💡 Related method:
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation()
-
Lifecycle Aware Beans are processed:
-
setBeanName()
(BeanNameAware
). 💡 Related method:BeanNameAware#setBeanName(String name)
-
setBeanClassLoader()
(BeanClassLoaderAware
). 💡 Related method:BeanClassLoaderAware#setBeanClassLoader(ClassLoader classLoader)
-
setBeanFactory()
(BeanFactoryAware
). 💡 Related method:BeanFactoryAware#setBeanFactory(BeanFactory factory)
-
setEnvironment()
(EnvironmentAware
). 💡 Related method:EnvironmentAware#setEnvironment(Environment environment)
-
setEmbeddedValueResolver()
(EmbeddedValueResolverAware
). 💡 Related method:EmbeddedValueResolverAware#setEmbeddedValueResolver(StringValueResolver resolver)
-
setResourceLoader()
,setApplicationEventPublisher()
,setMessageSource()
,setApplicationContext()
,setServletContext()
(if applicable).
-
-
Conditional Beans & Profiles Applied (
@Conditional
,@Profile
). 💡 Related method:Condition#matches()
,ConfigurableEnvironment#getActiveProfiles()
-
Post-processing before initialization:
💡 Related method:
BeanPostProcessor#postProcessBeforeInitialization()
-
Custom Initialization:
-
@PostConstruct
-
InitializingBean.afterPropertiesSet()
-
init-method
(if defined in@Bean
). 💡 Related method:InitializingBean#afterPropertiesSet()
,@PostConstruct
-
-
Post-processing after initialization:
💡 Related method:
BeanPostProcessor#postProcessAfterInitialization()
3️⃣ Application Startup Completion
-
ApplicationContext Refreshes:
- Dependency injection is completed.
- Fires
ContextRefreshedEvent
. 💡 Related method:AbstractApplicationContext#refresh()
-
Embedded Web Server Starts (if applicable):
- Tomcat, Jetty, or Undertow binds to a port.
-
ServletContextInitializer
andWebApplicationInitializer
execute for servlet-based apps. 💡 Related method:ConfigurableWebServerApplicationContext#start()
-
CommandLineRunner & ApplicationRunner Execute:
- Runs post-startup logic.
💡 Related method:
CommandLineRunner#run()
,ApplicationRunner#run()
- Runs post-startup logic.
💡 Related method:
-
ApplicationReadyEvent Fires:
- Application is fully initialized and ready to serve requests.
💡 Related method:
ApplicationListener#onApplicationEvent(ApplicationReadyEvent)
- Application is fully initialized and ready to serve requests.
💡 Related method:
4️⃣ Bean Destruction & Application Shutdown
-
Graceful Shutdown Begins:
- Controlled via
spring.lifecycle.timeout-per-shutdown-phase
. 💡 Related method:SpringApplication#setRegisterShutdownHook(true)
- Controlled via
-
Pre-Destruction Processing:
💡 Related method:
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction()
-
Custom Cleanup:
-
DisposableBean.destroy()
. -
@PreDestroy
method. - Custom
destroy-method
(if defined in@Bean
). 💡 Related method:DisposableBean#destroy()
,@PreDestroy
-
-
ApplicationContext Closes:
- Fires
ContextClosedEvent
. 💡 Related method:ConfigurableApplicationContext#close()
- Fires
-
SpringApplication.exit() can be used to set custom exit codes (
ExitCodeGenerator
). 💡 Related method:SpringApplication#exit()
5️⃣ Advanced Considerations (Optimizations & Monitoring)
✅ Lazy Initialization (@Lazy
) – Beans are only created when accessed.
💡 Related method: DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(false)
✅ Circular Dependency Handling – Use @Lazy
, setter injection, or @DependsOn
.
💡 Related method: AbstractAutowireCapableBeanFactory#doResolveDependency()
✅ FactoryBean Mechanism – Provides dynamic bean creation.
💡 Related method: FactoryBean#getObject()
✅ Spring Boot Actuator (if enabled):
-
/actuator/health
,/actuator/shutdown
,/actuator/metrics
. 💡 Related method:HealthIndicator#health()
✅ Performance Optimizations: - Reduce startup time with
spring.main.lazy-initialization=true
. - Tune garbage collection (
-XX:+UseG1GC
). ✅ Custom Application Listeners (ApplicationListener<>
) – Hook into startup/shutdown events. 💡 Related method:ApplicationListener#onApplicationEvent()
📌 Final Summary: Ultimate Execution Order
🟢 Bootstrapping: JVM → SpringApplication.run()
→ AutoConfiguration → Context Creation
🟠 Context Initialization: Beans Instantiated → Lifecycle Hooks → Dependency Injection
🔵 Application Startup: Web Server Starts → Runners Execute → Application Ready
🟣 Shutdown Phase: Pre-Destruction Callbacks → Cleanup → Context Closes
Top comments (0)