🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# SpringBoot – `CommandLineRunner`接口示例 > 原文: [https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/](https://howtodoinjava.com/spring-boot/command-line-runner-interface-example/) 在应用程序初始化后,Spring Boot 的[`CommandLineRunner`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/CommandLineRunner.html)接口仅在应用程序生存期内运行一次代码块。 ## 如何使用`CommandLineRunner` 您可以通过三种方式使用`CommandLineRunner`界面: #### 1)使用`CommandLineRunner`作为`@Component` 这很容易。 ```java @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` 这也是可能的。 示例代码如下: ```java @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` ```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** ```java @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`](https://docs.spring.io/spring-framework/docs/4.3.7.RELEASE/javadoc-api/org/springframework/core/annotation/Order.html?is-external=true)注解。 ```java @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 !!"); } } ``` 验证日志。 ```java 2017-03-08 13:55:04 - ApplicationStartupRunnerTwo run method Started !! 2017-03-08 13:55:04 - ApplicationStartupRunnerOne run method Started !! ``` ## 为什么要使用`CommandLineRunner`接口 * 命令行运行程序是一种有用的功能,可以在应用程序启动后立即执行仅需运行一次的各种类型的代码。 * 仅供参考,Spring Batch 依靠这些运行程序来触发作业的执行。 * 我们可以利用依赖注入来发挥优势,以便在`run()`方法实现中连接所需的任何依赖以及所需的任何方式。 学习愉快!