我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息。在Spring Boot中给我们提供了两个接口来帮助我们实现这样的需求。这两个接口就是我们今天要讲的
- CommandLineRunner
- ApplicationRunner,
他们的执行时机为容器启动完成的时候。使用起来非常简单。只需要实现CommandLineRunner或者ApplicationRunner接口。
如果是Spring 环境下,则可以参看本博的下面的文章讲如何在Spring环境做初始化操作的。
https://blog.csdn.net/bigtree_3721/article/details/103735767。
如果在SpringBoot应用启动的时候需要执行特定的动作,可以利用CommandLineRunner或者ApplicationRunner。实现了CommandLineRunner或者ApplicationRunner接口的Component会在 所有Spring Beans都初始化之后,SpringApplication.run()之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作。
共同点:
其一 执行时机都是在容器启动完成的时候进行执行;其二 这两个接口中都有一个run()方法;
如果程序里既有ApplicationRunner ,也有CommandLineRunner代码 ,
则ApplicationRunner 先运行,而CommandLineRunner 后运行。
不同点:
ApplicationRunner中run方法的参数为ApplicationArguments,
而CommandLineRunner接口中run方法的参数为String数组。
问题提出: 如果有多个实现类,而我们需要按照一定的顺序执行的话,那么应该怎么办呢?
解决方案:
方法1)可以在实现类上加上@Order注解指定执行的顺序;
方法2)可以在实现类上实现Ordered接口来标识。
需要注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。
下面展示了如何在程序中使用CommandLineRunner
和ApplicationRunner
。
Maven配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.concretepage</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-demo</name>
<description>Spring Boot Demo Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CommandLineRunner
CommandLineRunner
是个接口,有一个run()
方法。为了使用CommandLineRunner
我们需要创建一个类实现该接口并覆盖run()
方法。使用@Component
注解实现类。当SpringApplication.run()
启动spring boot程序时,启动完成之前,CommandLineRunner.run()
会被执行。CommandLineRunner
的run()
方法接收启动服务时传过来的参数。
run(String... args)
参数为String数组。
CommandLineRunnerBean.java
package com.concretepage.bean;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CommandLineRunnerBean implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineRunnerBean.class);
public void run(String... args) {
String strArgs = Arrays.stream(args).collect(Collectors.joining("|"));
logger.info("Application started with arguments:" + strArgs);
}
}
MyApplication.java
package com.concretepage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.concretepage.service.HelloService;
@SpringBootApplication
public class MyApplication {
private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args);
HelloService service = context.getBean(HelloService.class);
logger.info(service.getMessage());
}
}
我们也可以创建一个service。一旦Spring boot启动完成service就会执行。
这意味着SpringApplication.run()
执行完成后service的方法就会执行。
HelloService.java
package com.concretepage.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String getMessage(){
return "Hello World!";
}
}
现在使用带有参数的可执行jar运行程序。spring-boot-demo-0.0.1-SNAPSHOT.jar
为生成的jar文件。执行命令如下:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3
输出结果为:
2017-03-19 13:38:38.909 INFO 1036 --- [main] c.c.bean.CommandLineRunnerBean : Application started with arguments:data1|data2|data3
2017-03-19 13:38:38.914 INFO 1036 --- [main] com.concretepage.MyApplication : Started MyApplication in 1.398 seconds (JVM running for 1.82)
2017-03-19 13:38:38.915 INFO 1036 --- [main] com.concretepage.MyApplication : Hello World!
ApplicationRunner
ApplicationRunner
和CommandLineRunner
的作用相同。在SpringApplication.run()
完成spring boot启动之前,ApplicationRunner
的run()
方法会被执行。
run(ApplicationArguments args)
可以看到,CommandLineRunner.run()
接收String数组作为参数,而ApplicationRunner.run()
接收ApplicationArguments
作为参数。这些参数是启动spring boot程序时传给main()
方法的。
ApplicationRunnerBean.java
package com.concretepage.bean;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class ApplicationRunnerBean implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerBean.class);
@Override
public void run(ApplicationArguments arg0) throws Exception {
String strArgs = Arrays.stream(arg0.getSourceArgs()).collect(Collectors.joining("|"));
logger.info("Application started with arguments:" + strArgs);
}
}
创建可执行jar包并使用如下参数运行:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3
输出结果为:
2017-03-19 16:26:06.952 INFO 5004 --- [ main] c.c.bean.ApplicationRunnerBean : Application started with arguments:data1|data2|data3
2017-03-19 16:26:06.956 INFO 5004 --- [ main] com.concretepage.MyApplication : Started MyApplication in 1.334 seconds (JVM running for 1.797)
2017-03-19 16:26:06.957 INFO 5004 --- [ main] com.concretepage.MyApplication : Hello World!
CommandLineRunner和ApplicationRunner的执行顺序
在spring boot程序中,我们可以使用不止一个实现CommandLineRunner
和ApplicationRunner
的bean。为了有序执行这些bean的run()
方法,可以使用@Order
注解或Ordered
接口。例子中我们创建了两个实现CommandLineRunner
接口的bean和两个实现ApplicationRunner
接口的bean。我们使用@Order
注解按顺序执行这四个bean。
CommandLineRunnerBean1.java
@Component
@Order(1)
public class CommandLineRunnerBean1 implements CommandLineRunner {
public void run(String... args) {
System.out.println("CommandLineRunnerBean 1");
}
}
ApplicationRunnerBean1.java
@Component
@Order(2)
public class ApplicationRunnerBean1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("ApplicationRunnerBean 1");
}
}
CommandLineRunnerBean2.java
@Component
@Order(3)
public class CommandLineRunnerBean2 implements CommandLineRunner {
public void run(String... args) {
System.out.println("CommandLineRunnerBean 2");
}
}
ApplicationRunnerBean2.java
@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("ApplicationRunnerBean 2");
}
}
输出结果为:
CommandLineRunnerBean 1
ApplicationRunnerBean 1
CommandLineRunnerBean 2
ApplicationRunnerBean 2