我们在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为CommandLineRunner和ApplicationRunner。他们的执行时机为容器启动完成的时候。
这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。下面我写两个简单的例子,来看一下这两个接口的实现。
Spring boot的CommandLineRunner
接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
1、和@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注解一起使用
这种使用方式也相当的简单,参考代码如下:
@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();
}
}
三、ApplicationRunner接口注意:在实现
CommandLineRunner
接口时,run(String… args)
方法内部如果抛异常的话,会直接导致应用启动失败,所以,一定要记得将危险的代码放在try-catch
代码块里。
具体代码如下:
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* Created by zkn on 2016/8/12.
* 注意:一定要有@Component这个注解。要不然SpringBoot扫描不到这个类,是不会执行。
*/
@Component
public class TestImplApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(args);
System.out.println("这个是测试ApplicationRunner接口");
}
}
四、用@Order注解去设置多个CommandLineRunner或ApplicationRunner实现类的执行顺序
一个应用可能存在多个CommandLineRunner
接口实现类,如果我们想设置它们的执行顺序,可以使用 @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()方法里使用任何依赖,因为它们已经初始化好了;
如果你发现你的实现类没有按照你的需求执行,请看一下实现类上是否添加了Spring管理的注解(@Component)。