Clean • Professional
Spring Boot applications may appear simple, often starting with just one line:
SpringApplication.run(MyApplication.class, args);
However, behind this single line, Spring Boot executes a well-structured, multi-step startup process. Understanding this internal workflow is crucial for:
This guide explains everything about SpringApplication, startup phases, bean lifecycle, embedded server initialization, and events—perfect for students, developers, and professionals.
SpringApplication is the core bootstrap engine of Spring Boot. It is the orchestrator of the application startup process.
Responsibilities of SpringApplication
Think of SpringApplication as the brain and conductor of the Spring Boot startup engine.
Here’s the big picture of the Spring Boot startup process:
SpringApplication.run()
↓
SpringApplication initialization
↓
RunListeners.starting()
↓
prepareEnvironment()
↓
RunListeners.environmentPrepared()
↓
createApplicationContext()
↓
prepareContext()
↓
load() – Bean definition loading
↓
refresh() – Bean instantiation, wiring, auto-configuration
↓
RunListeners.started()
↓
RunListeners.running()
Each phase executes in a specific order with a well-defined responsibility.
The JVM executes the main() method:
publicstaticvoidmain(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
What happens here:
SpringApplication.run() triggers all internal phasesSpring Boot performs initial setup:
ApplicationContextInitializerSpringApplicationRunListenersAt this phase:
Only startup preparation occurs.
starting()Spring Boot notifies listeners that the application is starting:
RunListeners.starting()
Purpose:
At this moment:
prepareEnvironment() – Environment SetupSpring Boot prepares the Environment, which holds properties, profiles, and configuration sources.
Key steps:
StandardEnvironment or StandardServletEnvironment (depending on application type)application.properties / application.ymldev, prod)Property Priority (High → Low):
application.yml, application.properties)After this step, all configuration values are known and available for context preparation.
environmentPrepared()Listeners are notified that the environment is ready:
RunListeners.environmentPrepared()
Purpose:
At this stage:
createApplicationContext() – Context CreationSpring Boot now creates the ApplicationContext, the container for all beans.
Application Type → Context Mapping:
| Application Type | Context Type |
|---|---|
| Web MVC | AnnotationConfigServletWebServerApplicationContext |
| Reactive Web | AnnotationConfigReactiveWebServerApplicationContext |
| Non-web | AnnotationConfigApplicationContext |
Notes:
prepareContext() – Context ConfigurationThe ApplicationContext is prepared for bean registration:
At this stage:
postProcessApplicationContext() – Context CustomizationBefore bean instantiation, Spring Boot allows context customization:
BeanFactoryPostProcessor (e.g., ConfigurationPropertiesBindingPostProcessor)@Conditional beansThis ensures only relevant beans are created during startup.
load() – Bean Definition LoadingSpring Boot scans for all bean definitions:
@Component@Service@Repository@Controller@Configuration classes@Bean methodsImportant:
refresh() – Core Spring EngineThe heart of Spring Boot startup.
Steps inside refresh():
@Autowired dependencies are resolvedBean Lifecycle Simplified:
Create Bean
↓
Inject Dependencies
↓
@PostConstruct
↓
Readyto Use
After refresh(), the application is fully initialized.
started()RunListeners.started()
running() (Application Ready)RunListeners.running()
For web applications:
DispatcherServlet is registeredSpring Boot executes:
CommandLineRunner beansApplicationRunner beansPurpose: Custom logic after context initialization.
Throughout startup, Spring Boot notifies listeners:
| Event | Purpose |
|---|---|
starting | Before anything happens |
environmentPrepared | Environment is ready |
contextPrepared | Context is initialized, before refresh |
contextLoaded | Bean definitions loaded |
started | Context refreshed, embedded server running |
running | Application ready for requests |
failed | Startup failure occurred |
These events allow custom logging, monitoring, and extensions.
1. JVM starts
2. main() executes
3. SpringApplication.run()
4. RunListeners.starting()
5. prepareEnvironment()
6. RunListeners.environmentPrepared()
7. createApplicationContext()
8. prepareContext()
9. load() – bean definitions registered
10. postProcessApplicationContext() – customize context
11. refresh() – beans instantiated & injected, auto-config applied
12. RunListeners.started()
13. Embedded web server starts (if web app)
14. afterRefresh() – CommandLineRunner / ApplicationRunner executed
15. RunListeners.running() – application ready
refresh()@Conditional annotations control which beans are instantiatedApplicationContextInitializerCommandLineRunner / ApplicationRunnerSpringApplicationRunListenerSpringApplication is the brain of Spring BootSpring Boot may appear to start with a single line of code, but internally it runs a powerful, structured, and well-designed engine.
Understanding the SpringApplication internal workflow gives you:
Mastering the Spring Boot startup lifecycle transforms you from a user to an expert in Spring Boot.