SpringBoot – CommandLineRunner
接口示例
原文: https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/
在应用程序初始化后,Spring Boot 的CommandLineRunner
接口仅在应用程序生存期内运行一次代码块。
如何使用CommandLineRunner
您可以通过三种方式使用CommandLineRunner
界面:
1)使用CommandLineRunner
作为@Component
这很容易。
@Component
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunner run method Started !!");
}
}
2)在@SpringBootApplication
中实现CommandLineRunner
这也是可能的。 示例代码如下:
@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
接口的类。
ApplicationStartupRunner.java
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}
注册 ApplicationStartupRunner 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 ApplicationStartupRunner schedulerRunner() {
return new ApplicationStartupRunner();
}
}
重要的是要注意,如果在run(String... args)
方法内引发任何异常,这将导致上下文关闭并关闭应用程序。 因此,总是将有风险的代码放在try-catch
块中。
如果有多个CommandLineRunner
接口实现,请使用@Order
您可能具有CommandLineRunner
接口的多种实现。 默认情况下,spring boot 扫描其所有run()
方法并执行。 但是,如果要在其中强加一些顺序,请使用@Order
注解。
@Order(value=3)
@Component
class ApplicationStartupRunnerOne implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunnerOne run method Started !!");
}
}
@Order(value=2)
@Component
class ApplicationStartupRunnerTwo implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunnerTwo run method Started !!");
}
}
验证日志。
2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !!
2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !!
为什么要使用CommandLineRunner
接口
- 命令行运行程序是一种有用的功能,可以在应用程序启动后立即执行仅需运行一次的各种类型的代码。
- 仅供参考,Spring Batch 依靠这些运行程序来触发作业的执行。
- 我们可以利用依赖注入来发挥优势,以便在
run()
方法实现中连接所需的任何依赖以及所需的任何方式。
学习愉快!