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