Understanding Spring Boot Extension Points: Initializers, Listeners, Runners, BeanFactoryPostProcessor and BeanPostProcessor
This article explains the five major extension points in Spring Boot—ApplicationContextInitializer, ApplicationListener, Runner, BeanFactoryPostProcessor, and BeanPostProcessor—showing how they are discovered via SPI, where they are invoked during the startup lifecycle, and how to implement custom ones with code examples.
The author, a senior architect, walks through the internal startup process of a Spring Boot application, starting with the new SpringApplication(SpringbootExtensionPointApplication.class).run(args) call that creates a SpringApplication instance and triggers a series of initializers.
1. ApplicationContextInitializer – The article shows how Spring Boot loads seven default initializers from various spring.factories files. By adding a custom initializer to spring.factories , developers can have it executed during startup, as demonstrated by a printed log confirming the eighth initializer.
2. ApplicationListener – Listeners are another extension point configured via setListeners in the SpringApplication constructor. The author creates two generic listeners ( Starting and Started ) that react to ApplicationStartingEvent and ApplicationStartedEvent , respectively, and shows the call chain from SpringApplication.run() to listeners.starting() and listeners.started() .
3. Runner – After listeners, Spring Boot invokes callRunners() , which retrieves all beans implementing ApplicationRunner or CommandLineRunner from the context and executes them. A simple custom runner is added and its output is displayed, illustrating typical use cases such as resource cleanup or service registration after the application is fully started.
4. BeanFactoryPostProcessor – During refreshContext() , Spring calls invokeBeanFactoryPostProcessors() . The author adds a custom BeanFactoryPostProcessor that can modify BeanDefinition metadata before bean instances are created, and shows the resulting log output.
5. BeanPostProcessor – After bean instantiation, Spring registers BeanPostProcessor instances via registerBeanPostProcessors() . The article creates a custom post‑processor with postProcessAfterInitialization , demonstrates its execution, and notes that AOP in Spring is built on this mechanism.
Throughout the guide, the author includes diagrams (referenced as images in the original source) that map the flow of each extension point, helping readers visualize when and where to plug in custom logic during the Spring Boot startup lifecycle.
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.