(1)使用场景的提出;java
咱们在开发过程当中会有这样的场景:须要在容器启动的时候执行一些内容,好比:读取配置文件信息,数据库链接,删除临时文件,清除缓存信息,在Spring框架下是经过ApplicationListener监听器来实现的。在Spring Boot中给咱们提供了两个接口来帮助咱们实现这样的需求。这两个接口就是咱们今天要讲的CommandLineRunner和ApplicationRunner,他们的执行时机为容器启动完成的时候。spring
(2)共同点和区别;数据库
共同点:其一执行时机都是在容器启动完成的时候进行执行;其二这两个接口中都有一个run()方法;数组
不一样点:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。缓存
在实际项目开发中,咱们会有在项目服务启动的时候就去加载一些数据或作一些事情这样的需求。 app
为了解决这样的问题,Spring Boot 为咱们提供了一个方法,经过实现接口 CommandLineRunner 来实现。框架
很简单,只须要一个类就能够,无需其余配置。
eclipse
建立实现接口 com.kfit.runner.CommandLineRunner 的类,具体实现代码以下:ide
import
org.springframework.boot.CommandLineRunner;
import
org.springframework.stereotype.Component;
/**
* 服务启动执行
*
*/
@Component
public
class
MyStartupRunner1
implements
CommandLineRunner {
@Override
public
void
run(String... args)
throws
Exception {
System.out.println(
">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操做<<<<<<<<<<<<<"
);
}
}
Spring Boot应用程序在启动后,会遍历CommandLineRunner接口的实例并运行它们的run方法。也能够利用@Order注解(或者实现Order接口)来规定全部CommandLineRunner实例的运行顺序。工具
以下咱们使用@Order 注解来定义执行顺序。
import
org.springframework.boot.CommandLineRunner;
import
org.springframework.core.annotation.Order;
import
org.springframework.stereotype.Component;
/**
* 服务启动执行
*
*
*/
@Component
@Order
(value=
2
)
public
class
MyStartupRunner1
implements
CommandLineRunner {
@Override
public
void
run(String... args)
throws
Exception {
System.out.println(
">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操做 xxxxx <<<<<<<<<<<<<"
);
}
}
import
org.springframework.boot.CommandLineRunner;
import
org.springframework.core.annotation.Order;
import
org.springframework.stereotype.Component;
/**
* 服务启动执行
*
*
*/
@Component
@Order
(value=
1
)
public
class
MyStartupRunner2
implements
CommandLineRunner {
@Override
publicvoid run(String... args)
throws
Exception {
System.out.println(
">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操做 ****** <<<<<<<<<<<<<"
);
}
}
启动程序后,控制台输出结果为:
1
2
3
|
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操做 ***** <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操做 xxxxx <<<<<<<<<<<<<
|
根据控制台结果可判断,@Order 注解的执行优先级是按value值从小到大顺序。
1
2
3
4
5
|
@Override
publicvoid run(String... args)
throws
Exception {
System.out.println(Arrays.asList(args));
System.out.println(
">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操做11111111<<<<<<<<<<<<<"
);
}
|
这里的args就是程序启动的时候进行设置的:
SpringApplication.run(App.class, new String[]{"hello,","林峰"});
这里为了作演示,配置为固定值了,其实直接接收main中的args便可,那么在运行的时候,进行配置便可。
eclipse中给java应用传args参数的方法以下:
一、先写好Java代码,好比文件名为IntArrqy.java;
二、在工具栏或菜单上点run as下边有个Run Configuration;
三、在弹出窗口点选第二个标签arguments;
四、把你想输入的参数写在program argumenst就能够了,多个参数使用空格隔开。
完成后点run便可经过运行结果看到参数使用状况了。
ApplicationRunner接口也能够实现上述功能,代码以下:
@Component
@Order
(value =
0
)
class
StartupRunner2
implements
CommandLineRunner{
@Override
public
void
run(String... strings)
throws
Exception {
System.err.println(
">>服务启动执行,执行加载数据等操做<222222"
);
}
}
@Component
@Order
(value = -
1
)
class
StartupRunner3
implements
ApplicationRunner {
@Override
public
void
run(ApplicationArguments applicationArguments)
throws
Exception {
System.err.println(
">>服务启动执行,执行加载数据等操做<333333"
);
}
}
>>服务启动执行,执行加载数据等操做<
333333
>>服务启动执行,执行加载数据等操做<
222222