当前位置:网站首页>23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
2022-07-26 08:29:00 【csg103】
If you need to be in SpringApplication Run some specific code after startup , Then you can achieve ApplicationRunner or commandlinrun Interface . Both interfaces work in the same way , And provide a separate operation method , stay springapplication.run(.) Before completion , It will be called .
CommandLineRunner Interface provides access to application parameters , As a simple string array ,ApplicationRunner Provides ApplicationArguments.
The order of execution is SpringApplication Before starting , After most listeners . The listener can refer to the previous article .
ApplicationRunner Realization
@SpringBootApplication public 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) @Component class 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) @Component class MyApplicationRunner2 implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { System.out.println("-------------------" + this.toString()); } }
CommandLineRunner Realization
@SpringBootApplication public 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) @Component class 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) @Component class MyCommandLineRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("-------------------" + this.toString()); }
When a pair of implementations can be used @Order Sort and execute
边栏推荐
- Problems caused by slivereappbar
- Basic configuration of BGP
- 【时间复杂度空间复杂度】
- shell编程
- Run file command
- SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据
- Shell homework the next day
- Template summary
- 23.2 customizing the banner control display hidden banner modify banner
- When developing flutter, idea_ ID cannot solve the problem
猜你喜欢
![[time complexity, space complexity]](/img/f2/f82c7e0a6ab9f893023c2ddbac3431.png)
[time complexity, space complexity]

Beauty naked chat for a while, naked chat over the crematorium!

Mycat2 deploy master-slave MariaDB

Write common API tools swagger and redoc

Nodejs2day (modularization of nodejs, NPM download package, module loading mechanism)

The first ide overlord in the universe, replaced...

Burp suite Chapter 5 how to use burp target

2022-7-7 personal qualifying 4 competition experience

Super nice navigation page (static page)

Let's talk about the three core issues of concurrent programming.
随机推荐
matplotlib学习笔记
Special lecture 2 dynamic planning learning experience (should be updated for a long time)
Run file command
Exam summary on July 15, 2022
Redis进阶
Prefix infix suffix expression (written conversion)
Day 3 homework
Fluent uses protobuf
Mycat2 deploy master-slave MariaDB
Kotlin variables and constants
vscode 实用快捷键
关于期刊论文所涉及的一些概念汇编+期刊查询方法
Flitter imitates wechat long press pop-up copy recall paste collection and other custom customization
请问现在flinkcdc支持sqlserver实例名方式连接吗?
Shell programming
日常一记(11)--word公式输入任意矩阵
B title: razlika priority queue approach
Kotlin operator
A summary of practical websites that won't brighten people's eyes
Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)