Unveiling Spring’s 12 Core Startup Steps: A Deep Dive
This article walks through the twelve essential steps Spring performs during application startup, explaining each method—from prepareRefresh to finishRefresh—including bean factory preparation, post‑processor registration, and the final context refresh, all illustrated with diagrams and code snippets.
Preface
The entry point for Spring startup is AbstractApplicationContext#refresh . The process consists of twelve core methods, demonstrated using the default Spring Boot web application context implementation AnnotationConfigServletWebServerApplicationContext .
prepareRefresh
This step prepares the refresh by changing state flags and initializing property sources. It calls the template method initPropertySources , which the subclass overrides to add servlet‑related configuration to the Environment . It also validates required properties via getEnvironment().validateRequiredProperties() .
obtainFreshBeanFactory
This step refreshes and obtains the BeanFactory . Both refreshBeanFactory() and getBeanFactory() are abstract and implemented by the subclass, which simply sets an ID and returns a DefaultListableBeanFactory instance.
prepareBeanFactory
After obtaining a newly created BeanFactory , this step configures it:
Sets a ClassLoader for loading bean classes.
Registers a BeanExpressionResolver for SpEL processing.
Adds a ResourceEditorRegistrar to provide property editors for resource‑related types.
Registers several BeanPostProcessor s, such as ApplicationContextAwareProcessor , to handle Aware callbacks.
Calls beanFactory.ignoreDependencyInterface(EnvironmentAware.class) to prevent injection of EnvironmentAware beans.
Registers resolvable dependencies like beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory) so that @Resource BeanFactory injects the current factory.
postProcessBeanFactory
This template method is empty by default; the web subclass overrides it to add web‑specific configurations to the BeanFactory .
invokeBeanFactoryPostProcessors
This step invokes all BeanFactoryPostProcessor s. It first calls every BeanDefinitionRegistryPostProcessor (e.g., ConfigurationClassPostProcessor ) to parse configuration classes and register bean definitions, then invokes remaining BeanFactoryPostProcessor s for further customisation.
registerBeanPostProcessors
All BeanPostProcessor s are retrieved from the factory and registered, respecting their order. This completes the preparation of the BeanFactory for bean creation.
initMessageSource
Initialises internationalisation support by looking for a bean named messageSource ; if absent, a default implementation is used (Spring Boot auto‑configures one).
initApplicationEventMulticaster
Initialises the event multicaster by searching for an ApplicationEventMulticaster bean, otherwise creating a default one to publish application events.
onRefresh
The subclass overrides this template method to start the embedded web server.
registerListeners
Registers application listeners with the ApplicationEventMulticaster .
finishBeanFactoryInitialization
If a ConversionService bean exists, it is set on the factory. Then beanFactory.preInstantiateSingletons() instantiates all non‑lazy singleton beans and invokes SmartInitializingSingleton.afterSingletonsInstantiated() where applicable.
finishRefresh
The final step clears caches, initialises the LifecycleProcessor , invokes LifecycleProcessor#onRefresh (starting beans that implement SmartLifecycle ), and publishes a ContextRefreshedEvent . In a web environment, it also starts the web server and publishes a ServletWebServerInitializedEvent , which Spring Cloud uses for service registration.
Summary
The Spring startup sequence consists of initial preparation, a series of bean‑factory configuration steps, registration of post‑processors and listeners, initialisation of core infrastructure components, singleton pre‑instantiation, and finally context refresh and web server launch, after which the application is fully started.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.