Spring Boot Resource Initialization: CommandLineRunner, ApplicationRunner, @Order, @PostConstruct, InitializingBean, and ApplicationListener
This article explains several Spring Boot techniques for initializing resources at application startup, including CommandLineRunner, ApplicationRunner, @Order, @PostConstruct, InitializingBean, and ApplicationListener, and provides code examples and usage guidelines for each method.
When a Spring Boot project starts, you often need to initialize resources such as thread pools or load encryption certificates. The article introduces multiple ways to perform such initialization.
CommandLineRunner
Define an initialization class MyCommandLineRunner .
Implement the CommandLineRunner interface and its run() method.
Register the class as a Spring bean with @Component .
Example implementation:
package cn.zh.controller;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("项目初始化---------------11");
}
}The bean runs after all other beans are created but before SpringApplication.run() returns.
ApplicationRunner
Define an initialization class MyApplicationRunner .
Implement the ApplicationRunner interface and its run() method, which receives an ApplicationArguments object.
Register with @Component .
Example implementation:
package cn.zh.controller;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("项目初始化二---------");
}
}Both CommandLineRunner and ApplicationRunner achieve the same effect; the difference lies in the method signature: the former receives a simple String... args , while the latter provides richer ApplicationArguments .
@Order
If both CommandLineRunner and ApplicationRunner beans exist, the ApplicationRunner executes first. To control execution order, annotate the beans with @Order (lower values run earlier).
@Component
@Order(1)
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("项目初始化---------------11");
}
}
@Component
@Order(2)
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("项目初始化二---------");
}
@PostConstruct
public void init(){
System.out.println("@PostConstruct初始化");
}
}@PostConstruct
The @PostConstruct annotation can be placed on a no‑argument, void‑returning method to execute initialization logic immediately after the bean is constructed, without waiting for other beans. It runs only once and is the fastest way to perform simple initialization.
@PostConstruct
public void init(){
System.out.println("@PostConstruct初始化");
}InitializingBean
Implementing Spring's InitializingBean interface allows you to run code after all bean properties have been set, via the afterPropertiesSet() method.
@Component
public class MyListener1 implements InitializingBean {
@Autowired
private ShopInfoMapper shopInfoMapper;
@Override
public void afterPropertiesSet(){
System.out.println("项目启动OK");
}
}ApplicationListener
By implementing ApplicationListener , a bean can react to Spring events such as ApplicationReadyEvent . This provides another hook for resource initialization.
@Component
public class MyListener1 implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if (applicationEvent instanceof ApplicationReadyEvent) {
System.out.println("项目启动OK");
}
}
}Each of these mechanisms can be chosen based on the required initialization timing and dependency needs.
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.