Understanding Spring Boot Extension Points: Initializers, Listeners, Runners, BeanFactoryPostProcessor, and BeanPostProcessor
This article explains the core extension points of Spring Boot—including ApplicationContextInitializer, ApplicationListener, Runner, BeanFactoryPostProcessor, and BeanPostProcessor—showing how they are discovered via SPI, registered in spring.factories, and invoked during the application startup lifecycle.
1. ApplicationContextInitializer
When a Spring Boot application starts, it ultimately executes new SpringApplication(SpringbootExtensionPointApplication.class).run(args) . The static run method simply creates a SpringApplication instance and calls its run method, which sets up initializers and listeners. The initializer mechanism uses Spring’s SPI to locate implementations of ApplicationContextInitializer declared in spring.factories . After adding a custom initializer to spring.factories , it appears among the eight initializers printed during startup, confirming that the SPI registration works.
2. ApplicationListener
Listeners are another extension point discovered via the same SPI mechanism. They follow the observer pattern: an ApplicationEventMulticaster publishes events and registered ApplicationListener implementations receive them. The article creates two listeners— JackStartingApplicationListener and JackStartedApplicationListener —specifying the generic ApplicationEvent type, registers them in spring.factories , and demonstrates that the ApplicationStartingEvent and ApplicationStartedEvent are broadcast during SpringApplication.run() .
3. Runner
After listeners are invoked, Spring Boot calls callRunners , which retrieves all beans of type ApplicationRunner and CommandLineRunner from the context and executes them in a loop. The article adds a custom runner, runs the application, and shows the runner’s output, explaining that runners are useful for tasks that should execute after the application is fully started, such as resource cleanup or service registration.
4. BeanFactoryPostProcessor
During refreshContext() , Spring invokes invokeBeanFactoryPostProcessors() , an extension point that allows modification of BeanDefinition objects before the bean factory is fully refreshed. The author creates a custom BeanFactoryPostProcessor , registers it, and observes its effect on bean definitions during startup.
5. BeanPostProcessor
After bean instances are created via reflection, Spring registers and executes BeanPostProcessor implementations via registerBeanPostProcessors() . The two callback methods are postProcessBeforeInitialization and postProcessAfterInitialization ; the latter is commonly used for AOP proxy creation. The article implements a custom BeanPostProcessor , sets breakpoints, and shows that it is invoked from finishBeanFactoryInitialization() .
Conclusion
The article visualizes the complete extension‑point graph of Spring Boot, clarifying when each hook is called and helping developers decide which type of extension point to implement for a given scenario.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.