SpringBoot之CommandLineRunner接口和ApplicationRunner接口
2018-02-27 17:00
549 查看
我们在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为CommandLineRunner和ApplicationRunner。他们的执行时机为容器启动完成的时候。这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。下面我写两个简单的例子,来看一下这两个接口的实现。
public class TestImplCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("<<<<<<<<<<<<这个是测试CommandLineRunn接口>>>>>>>>>>>>>>");
}
}
public class TestImplApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(args);
System.out.println("这个是测试ApplicationRunner接口");
}
}
@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<");
}
}
@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<");
}
}
启动程序后,控制台输出结果为:
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<
CommandLineRunner
@Componentpublic class TestImplCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("<<<<<<<<<<<<这个是测试CommandLineRunn接口>>>>>>>>>>>>>>");
}
}
ApplicationRunner接口
@Componentpublic 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值从小到大依次执行。@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<");
}
}
@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<");
}
}
启动程序后,控制台输出结果为:
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<
相关文章推荐
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
- SpringBoot之CommandLineRunner接口
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口区别
- springboot之启动加载数据 CommandLineRunner 和ApplicationRunner
- SpringBoot源码分析之CommandLineRunner、ApplicationRunner
- Spring Boot 之 CommandLineRunner,ApplicationRunner
- Spring boot CommandLineRunner接口使用例子
- springboot系列教程(二)——Banner、CommandLineRunner、Arguments、Exit
- SpringBoot之启动加载数据 CommandLineRunner(G)
- Spring-boot中添加commandLineRunner之后,写单元测试会自动执行commandLineRunner的解决方案
- 102. Spring Boot之CommandLineRunner和ApplicationRunner【从零开始学Spring Boot】
- springboot启动的相关配置与CommandLineRunner
- spring-boot restful接口学习(1)
- 详解Spring Boot实战之Filter实现使用JWT进行接口认证
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- [置顶] 【微框架实战】Maven+SpringBoot 短信接口详解与Demo
- Spring Boot 启动载入数据 CommandLineRunner
- 运用RestExpress+Springboot实现web接口
- spring boot 中使用swagger 来自动生成接口文档
- [增删改查] SpringBoot 整合 MongoDB 之 MongoTemplate 实现 CRUD、分页接口