Application Initializer and Listener Sorting
The framework begins by ordering implementations of ApplicationContextInitializer and ApplicationListener according to their priority or defined order. This ensures predictable behavior when several extensions modify the context.
Assembling Run Listeners
A SpringApplicationRunListener instance is created to monitor lifecycle events. The first event, starting(), is published immediately to indicate the launch phase, followed by construction of ApplicationArguments from command-line inputs.
Building and Refining the Environment
The prepareEnvironment step gathers initial property sources, which include:
StubPropertySourcefor servlet config and context init parametersPropertiesPropertySourcewrapping system propertiesSystemEnvironmentPropertySourcewith OS environment variables
After publishing the environmentPrepared event, additional contributors augment the property source list. The final environment typically holds:
configurationPropertiesbootstrapmap entries- system properties and environment variables
randomvalue sourcespringCloudClientHostInfo- externalized configuration from
bootstrap.yml
Creating the Application Context
A ConfigurableApplicationContext object is instanitated via its constructor. At this point, no beans are loaded—only the base structure exists.
Context Preparation (prepareContext)
This stage wires the environment, enhances the context, and broadcasts events. The method accepts the context, environment, listeners, arguments, and a banner instance.
- Environment injection:
context.setEnvironment(environment)binds the assembled environment. - Context augmentation:
postProcessApplicationContextcustomizes the bean factory by setting:beanNameGeneratorfor naming strategiesresourceLoaderfor file resolution- a type conversion service for data transformation
- Initializer execution: All registered
ApplicationContextInitializerinstances are applied, allowing programmatic modification of the context. - Event publication:
listeners.contextPrepared(context)broadcasts anApplicationContextInitializedEvent. - Bean factory retrieval: The
ConfigurableListableBeanFactoryis obtained from the context. - Singleton registration:
springApplicationArgumentsis registered as a singleton bean. - Configuration flags: Bean definition overriding is explicitly allowed.
- Post-processor registration: A
LazyInitializationBeanFactoryPostProcessoris added as aBeanFactoryPostProcessor.
Source Aggregation and Loading
All configured source classes are collected, with special handling for BootstrapImportSelectorConfiguration. This selector merges BootstrapConfiguration entries from spring.factories with the property spring.cloud.bootstrap.sources, sorts them, and returns the combined list as bean definitions.
The loading process dispatches based on source type. A BeanDefinitionLoader is created, configured with the bean name generator, resource loader, and environment, then executed.
The internal loader handles each source polymorphically: registering classes, scanning packages, or loading bean definitions from resources.
// Type-based loading dispatch
int loadOne(Object candidate) {
Objects.requireNonNull(candidate, "Source must not be null");
if (candidate instanceof Class) {
return register((Class<?>) candidate);
}
if (candidate instanceof Resource) {
return loadFromResource((Resource) candidate);
}
if (candidate instanceof Package) {
return scanPackage((Package) candidate);
}
if (candidate instanceof CharSequence) {
return loadFromName(candidate.toString());
}
throw new IllegalArgumentException("Unsupported source type: " + candidate.getClass());
}
After all sources are processed, listeners.contextLoaded(context) signals that the context preparation is complete.
Bean Definition Registry
The central registry for bean definitions is the DefaultListableBeanFactory, which manages metadata and relationships before instantiation.