Springboot監聽啟動事件


特別提示:本人博客部分有參考網絡其他博客,但均是本人親手編寫過並驗證通過。如發現博客有錯誤,請及時提出以免誤導其他人,謝謝!歡迎轉載,但記得標明文章出處: http://www.cnblogs.com/mao2080/

1、問題描述

  在排查rabbitmq消息堆積問題時,單獨將自己封裝的SDK拿出來發現死活都注冊不上去,結果發現是啟動順序導致的,需要實現ApplicationListener接口。

2、解決方案

1、實現ApplicationListener

1 @Component
2 public class MyListener implements ApplicationListener<ApplicationReadyEvent> {
3 
4     public void onApplicationEvent(ApplicationReadyEvent arg0) {
5         System.out.println("Spring容器啟動好了...");
6     }
7 
8 }
2、實現 CommandLineRunner
1 @Component
2 public class Runner implements CommandLineRunner{
3 
4     @Override
5     public void run(String... strings) throws Exception {
6         System.out.println("CommandLineRunner...");
7     }
8 
9 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM