23.8 Using the ApplicationRunner or CommandLineRunner 实现ApplicationRunner 和CommandLineRunner
来源:互联网 发布:itools pro for mac 编辑:程序博客网 时间:2024/06/06 03:53
如果您需要在SpringApplication启动之后运行一些特定的代码,那么您可以实现ApplicationRunner或commandlinrun 接口。这两个接口都以相同的方式工作,并提供一个单独的运行方法,在springapplication.run(.)完成之前,它将被调用。
CommandLineRunner
接口提供对应用程序参数的访问,作为一个简单的字符串数组,ApplicationRunner
提供了ApplicationArguments。
执行顺序在SpringApplication启动之前,在大部分监听器之后。监听器可参考前面的文章。
ApplicationRunner实现
@SpringBootApplicationpublic class MyApplicationRunner { public static void main(String[] args) { SpringApplication application = new SpringApplication(Application.class); application.addListeners(new ApplicationListenerStarted()); application.addListeners(new ApplicationListenerEnvironmentPrepared()); application.addListeners(new ApplicationListenerPrepared()); application.addListeners(new ApplicationListenerReadyEvent()); application.addListeners(new ApplicationListenerFailed()); application.run(args); }}@Order(2)@Componentclass MyApplicationRunner1 implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { StringBuffer sf =new StringBuffer(""); for(String str :applicationArguments.getSourceArgs()){ sf.append(str); } System.out.println("-------------------" + this.toString()+"----"+sf); }}@Order(1)@Componentclass MyApplicationRunner2 implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { System.out.println("-------------------" + this.toString()); }}
CommandLineRunner
实现
@SpringBootApplicationpublic class MyCommandLineRunner { public static void main(String[] args) { String[] aa ={"0","1"}; SpringApplication application = new SpringApplication(Application.class); application.addListeners(new ApplicationListenerStarted()); application.addListeners(new ApplicationListenerEnvironmentPrepared()); application.addListeners(new ApplicationListenerPrepared()); application.addListeners(new ApplicationListenerReadyEvent()); application.addListeners(new ApplicationListenerFailed()); application.run(aa); }}@Order(2)@Componentclass MyCommandLineRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { StringBuffer sf =new StringBuffer(""); for(String str :args){ sf.append(str); } System.out.println("-------------------" + this.toString()+ "---"+sf); }}@Order(1)@Componentclass MyCommandLineRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("-------------------" + this.toString()); }
当有对个实现可以使用@Order进行排序执行
阅读全文
0 0
- 23.8 Using the ApplicationRunner or CommandLineRunner 实现ApplicationRunner 和CommandLineRunner
- Spring Boot CommandLineRunner和ApplicationRunner
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
- springboot之启动加载数据 CommandLineRunner 和ApplicationRunner
- SpringBoot源码分析之CommandLineRunner、ApplicationRunner
- Spring Boot 启动时执行特殊的代码使用CommandLineRunner、ApplicationRunner
- CommandLineRunner详解
- spring boot CommandLineRunner启动加载数据库实现
- SpringBoot通过CommandLineRunner实现参数的初始化
- Spring-boot中ApplicationRunner源码
- @SpringBootApplication and CommandLineRunner
- 启动自执行 CommandLineRunner
- SpringBoot18 之 CommandLineRunner加载数据
- 15. CommandLineRunner 启动加载数据
- SpringBoot:spring boot CommandLineRunner启动
- springboot实战之整合CommandLineRunner
- Spring Boot 启动加载数据 CommandLineRunner
- Spring Boot 启动加载数据 CommandLineRunner
- 如何开启BBR算法优化ss
- 自定义单选框样式
- JSON 数据使用
- linux系统和windows系统数据格式问题
- 路由器和交换机的区别,路由器怎么当交换机用?
- 23.8 Using the ApplicationRunner or CommandLineRunner 实现ApplicationRunner 和CommandLineRunner
- SparkContext源码解读
- 欢迎使用CSDN-markdown编辑器
- 计算机视觉目标检测流程
- 现代浏览器的工作原理
- 堆栈队列实例
- spring官网下载spring框架
- 浅谈web网站架构演变过程
- Button控件的CommandName属性和OnCommand事件的使用以及对数据的绑定