当前位置:网站首页>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
边栏推荐
- Apple's tough new rule: third-party payment also requires a percentage, and developers lose a lot!
- General Dao interface design
- Flutter compilation fails
- QT uses QSS to make a beautiful login interface (hand-in-hand teaching)
- Problems caused by slivereappbar
- JS工具函数大全
- QSS add resource file of QT
- Run file command
- Use of room database in kotlin
- Regular expression job
猜你喜欢
![[time complexity, space complexity]](/img/f2/f82c7e0a6ab9f893023c2ddbac3431.png)
[time complexity, space complexity]

SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据

Burp suite Chapter 4 advanced options for SSL and proxy

How to safely delete a useless activity in Android studio

QT note 1

Flitter imitates wechat long press pop-up copy recall paste collection and other custom customization

OSPF summary

QSS add resource file of QT

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

2022-7-9 personal qualifying 6 competition experience
随机推荐
C# 获取选择文件信息
2022/7/11 exam summary
When developing flutter, idea_ ID cannot solve the problem
BGP -- Border Gateway Protocol
The most complete network: detailed explanation of six constraints of MySQL
2022-024ARTS:最长有效括号
Matplotlib learning notes
shell编程
Apple's tough new rule: third-party payment also requires a percentage, and developers lose a lot!
苹果强硬新规:用第三方支付也要抽成,开发者亏大了!
Burp suite Chapter 9 how to use burp repeater
Flitter imitates wechat long press pop-up copy recall paste collection and other custom customization
Template summary
Write common API tools swagger and redoc
Storage of drawings (refined version)
Redis进阶
Basic configuration of BGP
Understand microservices bit by bit
mysql函数汇总之日期和时间函数
Zroi easy sum (generating function, block, DP, combination, polynomial)