Understanding Spring Boot Extension Points: Initializers, Listeners, Runners, BeanFactoryPostProcessor, and BeanPostProcessor
This article explains the five major Spring Boot extension points—ApplicationContextInitializer, ApplicationListener, Runner, BeanFactoryPostProcessor, and BeanPostProcessor—showing how to register custom implementations via SPI, where they are invoked in the startup flow, and providing code examples for each.
1. ApplicationContextInitializer
When a Spring Boot application starts, new SpringApplication(SpringbootExtensionPointApplication.class).run(args) creates a SpringApplication object and invokes its run method. The method prepares the context by loading all ApplicationContextInitializer implementations registered in spring.factories . These initializers can modify the ConfigurableApplicationContext before beans are loaded.
2. ApplicationListener
Listeners are another extension point that follows the observer pattern. Spring registers ApplicationListener implementations (also via spring.factories ) and publishes events through ApplicationEventMulticaster . Custom listeners such as JackStartingApplicationListener and JackStartedApplicationListener receive ApplicationStartingEvent and ApplicationStartedEvent respectively, allowing developers to execute logic at precise moments of the startup lifecycle.
3. Runner
After the application context is refreshed, Spring calls all ApplicationRunner and CommandLineRunner beans via the callRunners method. These runners run after the application is fully started, making them suitable for tasks such as resource cleanup, registration with external services, or any post‑startup initialization.
4. BeanFactoryPostProcessor
During refreshContext() , Spring invokes invokeBeanFactoryPostProcessors() . Custom BeanFactoryPostProcessor implementations can modify BeanDefinition metadata before any bean instances are created, enabling programmatic alteration of bean properties, property sources, or even bean definitions themselves.
5. BeanPostProcessor
After a bean is instantiated via reflection, Spring calls the postProcessBeforeInitialization and postProcessAfterInitialization methods of all registered BeanPostProcessor beans. This is the core mechanism behind features such as AOP proxies. Developers can implement custom post‑processors to wrap beans, inject additional behavior, or perform validation after bean creation.
All five extension points are discovered through Spring’s Service Provider Interface (SPI) mechanism. By adding the fully qualified class names to META-INF/spring.factories , Spring Boot automatically picks them up and integrates them into the startup sequence, as demonstrated by the screenshots and code snippets throughout the article.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.