前言
Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
如何使用CommandLineRunner接口
我们可以用以下三种方式去使用CommandLineRunner接口:
1)和@Component注解一起使用
这种使用方式相当简便,如下所示:
@Component
public class Applicati upRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info(\"Applicati upRunner run method Started !!\");
}
}
2)和@SpringBootApplication注解一起使用
这种使用方式也相当的简单,参考代码如下:
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
logger.info(\"Application Started !!\");
}
}
3)声明一个实现了CommandLineRunner接口的Bean
这种方式其实也大同小异,就是在SpringBootApplication里定义一个Bean,改Bean实现了CommandLineRunner接口,参考代码如下:
Applicati upRunner.java
public class Applicati upRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info(\"Application Started !!\");
}
}
注册Applicati upRunner bean
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@Bean
public Applicati upRunner schedulerRunner() {
return new Applicati upRunner();
}
}
注意:在实现
CommandLineRunner接口时,run(String… args)方法内部如果抛异常的话,会直接导致应用启动失败,所以,一定要记得将危险的代码放在try-catch代码块里。
用@Order注解去设置多个CommandLineRunner实现类的执行顺序
一个应用可能存在多个CommandLineRunner接口实现类,如果我们想设置它们的执行顺序,可以使用 @Order实现
@Order(value=3)
@Component
class Applicati upRunnerOne implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info(\"Applicati upRunnerOne run method Started !!\");
}
}
@Order(value=2)
@Component
class Applicati upRunnerTwo implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info(\"Applicati upRunnerTwo run method Started !!\");
}
}
输出日志:
2017-03-08 13:55:04 - Applicati upRunnerTwo run method Started !!
2017-03-08 13:55:04 - Applicati upRunnerOne run method Started !!
为什么要使用CommandLineRunner接口
- 实现在应用启动后,去执行相关代码逻辑,且只会执行一次;
- spring batch批量处理框架依赖这些执行器去触发执行任务;
-
我们可以在run()方法里使用任何依赖,因为它们已经初始化好了;
继续阅读与本文标签相同的文章
-
CMU 15-721 16-服务器端的逻辑执行 Server -side Logic Execution
2026-05-18栏目: 教程
-
谷歌搜索广告出价方式
2026-05-18栏目: 教程
-
印度5G建设即将开始,是屈服于美国的施压,还是选择跟华为合作?
2026-05-18栏目: 教程
-
系列文章:云原生Kubernetes日志落地方案
2026-05-18栏目: 教程
-
QQ浏览器正孵化“用户增长团队”,解读中国浏览器行业发展趋势
2026-05-18栏目: 教程
