SpringBoot事件監(jiān)聽的4種實現(xiàn)方式
閱讀本文大概需要 2.8 分鐘。
來自:blog.csdn.net/ignorewho/article/details/80702827
手工向ApplicationContext中添加監(jiān)聽器 將監(jiān)聽器裝載入spring容器 在application.properties中配置監(jiān)聽器 通過@EventListener注解實現(xiàn)事件監(jiān)聽
自定義事件:繼承自ApplicationEvent抽象類,然后定義自己的構造器 自定義監(jiān)聽:實現(xiàn) ApplicationListener接口,然后實現(xiàn)onApplicationEvent方法
方式1
public?class?MyListener1?implements?ApplicationListener<MyEvent>
{
?Logger?logger?=?Logger.getLogger(MyListener1.class);
?
?public?void?onApplicationEvent(MyEvent?event)
?{
??logger.info(String.format("%s監(jiān)聽到事件源:%s.",?MyListener1.class.getName(),?event.getSource()));
?}
}
@SpringBootApplication
public?class?LisenterApplication
{
?public?static?void?main(String[]?args)
?{
??ConfigurableApplicationContext?context?=?SpringApplication.run(LisenterApplication.class,?args);
??//裝載監(jiān)聽
??context.addApplicationListener(new?MyListener1());
?}
}
方式2
@Component
public?class?MyListener2?implements?ApplicationListener<MyEvent>
{
?Logger?logger?=?Logger.getLogger(MyListener2.class);
?
?public?void?onApplicationEvent(MyEvent?event)
?{
??logger.info(String.format("%s監(jiān)聽到事件源:%s.",?MyListener2.class.getName(),?event.getSource()));
?}
}
方式3
public?class?MyListener3?implements?ApplicationListener<MyEvent>
{
?Logger?logger?=?Logger.getLogger(MyListener3.class);
?
?public?void?onApplicationEvent(MyEvent?event)
?{
??logger.info(String.format("%s監(jiān)聽到事件源:%s.",?MyListener3.class.getName(),?event.getSource()));
?}
}
application.properties中配置監(jiān)聽context.listener.classes=com.listener.MyListener3
方式4
@Component
public?class?MyListener4
{
?Logger?logger?=?Logger.getLogger(MyListener4.class);
?
?@EventListener
?public?void?listener(MyEvent?event)
?{
??logger.info(String.format("%s監(jiān)聽到事件源:%s.",?MyListener4.class.getName(),?event.getSource()));
?}
}
@SuppressWarnings("serial")
public?class?MyEvent?extends?ApplicationEvent
{
?public?MyEvent(Object?source)
?{
??super(source);
?}
}
@SpringBootApplication
public?class?LisenterApplication
{
?public?static?void?main(String[]?args)
?{
??ConfigurableApplicationContext?context?=?SpringApplication.run(LisenterApplication.class,?args);
??//裝載事件
??context.addApplicationListener(new?MyListener1());
??//發(fā)布事件
??context.publishEvent(new?MyEvent("測試事件."));
?}
}
2018-06-15 10:51:20.198 INFO 4628 ---?[?????????? main] com.listener.MyListener3 ????????????????: com.listener.MyListener3監(jiān)聽到事件源:測試事件..
2018-06-15 10:51:20.198 INFO 4628 ---?[?????????? main] com.listener.MyListener4 ????????????????: com.listener.MyListener4監(jiān)聽到事件源:測試事件..
2018-06-15 10:51:20.199 INFO 4628 ---?[?????????? main] com.listener.MyListener2 ????????????????: com.listener.MyListener2監(jiān)聽到事件源:測試事件..
2018-06-15 10:51:20.199 INFO 4628 ---?[?????????? main] com.listener.MyListener1 ????????????????: com.listener.MyListener1監(jiān)聽到事件源:測試事件..
https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener
終于實現(xiàn)了 SpringBoot+WebSocket實時監(jiān)控異常....
內(nèi)容包含Java基礎、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊列、高性能緩存、反射、Spring全家桶原理、微服務、Zookeeper、數(shù)據(jù)結構、限流熔斷降級......等技術棧!
?戳閱讀原文領取!? ? ? ? ? ? ? ??? ??? ? ? ? ? ? ? ? ? ?朕已閱?
評論
圖片
表情

