在spring boot应用中,我们可以在程序启动之前执行任何任务。为了达到这个目的,我们需要使用CommandLineRunner
或ApplicationRunner
接口创建bean,spring boot会自动监测到它们。这两个接口都有一个run()
方法,在实现接口时需要覆盖该方法,并使用@Component
注解使其成为bean。CommandLineRunner
和ApplicationRunner
的作用是相同的。不同之处在于CommandLineRunner
接口的run()
方法接收String数组作为参数,而ApplicationRunner
接口的run()
方法接收ApplicationArguments
对象作为参数。当程序启动时,我们传给main()
方法的参数可以被实现CommandLineRunner
和ApplicationRunner
接口的类的run()
方法访问。我们可以创建多个实现CommandLineRunner
和ApplicationRunner
接口的类。为了使他们按一定顺序执行,可以使用@Order
注解或实现Ordered
接口。
CommandLineRunner
和ApplicationRunner
接口的run()
方法在SpringApplication
完成启动时执行。启动完成之后,应用开始运行。CommandLineRunner
和ApplicationRunner
的作用是在程序开始运行前执行任务或记录信息。
下面展示了如何在程序中使用CommandLineRunner
和ApplicationRunner
。
这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。下面我写两个简单的例子,来看一下这两个接口的实现。
CommandLineRunner
具体代码如下:
-
import org.springframework.boot.CommandLineRunner;
-
import org.springframework.stereotype.Component;
-
/**
-
* Created by zkn on 2016/8/12.
-
*/
-
@Component
-
public class TestImplCommandLineRunner implements CommandLineRunner {
-
@Override
-
public void run(String... args) throws Exception {
-
System.out.println("这个是测试CommandLineRunn接口");
-
}
-
}
执行结果为:
ApplicationRunner接口
具体代码如下:
-
package com.zkn.learnspringboot.runner;
-
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注解
如果有多个实现类,而你需要他们按一定顺序执行的话,可以在实现类上加上@Order注解。@Order(value=整数值)。SpringBoot会按照@Order中的value值从小到大依次执行。
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");
}
}
- 1
ApplicationRunnerBean2.java
@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("ApplicationRunnerBean 2");
}
}
- 1
输出结果为:
CommandLineRunnerBean 1
ApplicationRunnerBean 1
CommandLineRunnerBean 2
ApplicationRunnerBean 2
Tips
如果你发现你的实现类没有按照你的需求执行,请看一下实现类上是否添加了Spring管理的注解(@Component)。