Spring引导的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
您可以定义一个bean,在SpringBootApplication
该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()
方法实现中连接我们需要的任何依赖项以及我们想要的任何方式。