Master Spring Boot 3 Initialization: 7 Practical Techniques Explained
This article introduces seven Spring Boot 3 initialization mechanisms—including @PostConstruct, InitializingBean, @Bean initMethod, ApplicationRunner, ContextRefreshedEvent, SmartInitializingSingleton, and SmartLifecycle—detailing their usage, code examples, execution order, and suitable scenarios for effective application startup.
In Spring Boot 3.4.2, initialization can be performed using several mechanisms that run at different stages of the application lifecycle.
1. Introduction
Initialization tasks such as loading configuration, pre‑warming caches, or registering listeners are executed before the application becomes fully operational.
2. Practical Cases
2.1 @PostConstruct
The annotation marks a method to be executed after dependency injection is complete.
<code>@Service
public class CommonService {}
@Component
public class PackComponent {
@Resource
private CommonService commonService;
public PackComponent() {
System.err.printf("Constructor, commonService: %s%n", this.commonService);
}
@PostConstruct
public void init() {
System.err.printf("@PostConstruct init, commonService: %s%n", this.commonService);
}
}
</code>Output when the container starts:
<code>Constructor, commonService: null
@PostConstruct init, commonService: CommonService@7cfb4736
</code>Applicable scenario: Simple bean‑level initialization such as cache pre‑warming.
2.2 InitializingBean
Implementing InitializingBean and overriding afterPropertiesSet() provides similar functionality.
<code>@Component
public class PackComponent implements InitializingBean {
@Resource
private CommonService commonService;
@Override
public void afterPropertiesSet() throws Exception {
System.err.printf("InitializingBean init, commonService: %s%n", this.commonService);
}
}
</code>Output:
<code>Constructor, commonService: null
InitializingBean init, commonService: CommonService@3811510
</code>Note: Official recommendation prefers @PostConstruct over this interface.
2.3 @Bean initMethod
For third‑party classes that cannot be annotated, define an @Bean with initMethod .
<code>public class ThirdPartyComponent {
public void init() {
System.err.println("ThirdPartyComponent init...");
}
}
@Bean(initMethod = "init")
public ThirdPartyComponent thirdPartyComponent() {
return new ThirdPartyComponent();
}
</code>Output:
<code>ThirdPartyComponent init...
</code>Applicable scenario: Initializing external libraries.
2.4 ApplicationRunner / CommandLineRunner
Implement either interface to run code after the ApplicationContext is fully refreshed.
<code>@Component
public class PackComponent implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.err.println("CommandLineRunner init action...");
}
}
</code>Both runners execute after the context is ready; the difference lies in how command‑line arguments are received.
Applicable scenario: Global tasks such as data initialization.
2.5 ContextRefreshedEvent
Listening to this event allows execution right after the context refresh completes.
<code>@Component
public class PackComponent implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
System.err.println("Context refreshed, performing init...");
}
}
</code>Suitable for actions that must run after all beans are created.
2.6 SmartInitializingSingleton
The callback afterSingletonsInstantiated() runs after all singleton beans are instantiated but before any ApplicationRunner executes.
<code>@Component
public class PackComponent implements SmartInitializingSingleton {
@Override
public void afterSingletonsInstantiated() {
System.err.println("SmartInitializingSingleton init");
}
}
</code>Ideal for initialization that depends on other singleton beans.
2.7 SmartLifecycle
Implementing SmartLifecycle provides start/stop control for components.
<code>@Component
public class PackComponent implements SmartLifecycle {
private volatile boolean running;
@Override
public void start() {
System.err.println("SmartLifecycle start");
this.running = true;
}
@Override
public void stop() {
this.running = false;
}
@Override
public boolean isRunning() {
return this.running;
}
}
</code>Used for external resource management, cache pre‑warming, or asynchronous processing.
All the above methods can be combined according to specific requirements to achieve fine‑grained control over Spring Boot application startup.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.