1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot事件監(jiān)聽的4種實(shí)現(xiàn)方式

        共 3414字,需瀏覽 7分鐘

         ·

        2021-12-18 16:17

        程序員的成長之路
        互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享?
        關(guān)注


        閱讀本文大概需要 2.8 分鐘。

        來自:blog.csdn.net/ignorewho/article/details/80702827

        springboot進(jìn)行事件監(jiān)聽有四種方式:
        1. 手工向ApplicationContext中添加監(jiān)聽器
        2. 將監(jiān)聽器裝載入spring容器
        3. 在application.properties中配置監(jiān)聽器
        4. 通過@EventListener注解實(shí)現(xiàn)事件監(jiān)聽
        講到事件監(jiān)聽,這里我們說下自定義事件和自定義監(jiān)聽器類的實(shí)現(xiàn)方式:
        • 自定義事件:繼承自ApplicationEvent抽象類,然后定義自己的構(gòu)造器
        • 自定義監(jiān)聽:實(shí)現(xiàn)ApplicationListener接口,然后實(shí)現(xiàn)onApplicationEvent方法
        下面講下4種事件監(jiān)聽的具體實(shí)現(xiàn)

        方式1

        首先創(chuàng)建MyListener1類
        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()));
        ?}
        }
        然后在springboot應(yīng)用啟動(dòng)類中獲取ConfigurableApplicationContext上下文,裝載監(jiān)聽
        @SpringBootApplication
        public?class?LisenterApplication
        {
        ?public?static?void?main(String[]?args)
        ?
        {
        ??ConfigurableApplicationContext?context?=?SpringApplication.run(LisenterApplication.class,?args);
        ??//裝載監(jiān)聽
        ??context.addApplicationListener(new?MyListener1());
        ?}
        }

        方式2

        創(chuàng)建MyListener2類,并使用@Component注解將該類裝載入spring容器中
        @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

        首先創(chuàng)建MyListener3類
        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

        創(chuàng)建MyListener4類,該類無需實(shí)現(xiàn)ApplicationListener接口,使用@EventListener裝飾具體方法
        @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);
        ?}
        }
        進(jìn)行測(cè)試(在啟動(dòng)類中加入發(fā)布事件的邏輯):
        @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("測(cè)試事件."));
        ?}
        }
        啟動(dòng)后,日志打印如下:
        2018-06-15 10:51:20.198 INFO 4628 ---?[?????????? main] com.listener.MyListener3 ????????????????: com.listener.MyListener3監(jiān)聽到事件源:測(cè)試事件..
        2018-06-15 10:51:20.198 INFO 4628 ---?[?????????? main] com.listener.MyListener4 ????????????????: com.listener.MyListener4監(jiān)聽到事件源:測(cè)試事件..
        2018-06-15 10:51:20.199 INFO 4628 ---?[?????????? main] com.listener.MyListener2 ????????????????: com.listener.MyListener2監(jiān)聽到事件源:測(cè)試事件..
        2018-06-15 10:51:20.199 INFO 4628 ---?[?????????? main] com.listener.MyListener1 ????????????????: com.listener.MyListener1監(jiān)聽到事件源:測(cè)試事件..
        由日志打印可以看出,SpringBoot四種事件的實(shí)現(xiàn)方式監(jiān)聽是有序的
        完整的代碼路徑:
        https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener
        推薦閱讀:

        手把手復(fù)現(xiàn)了 Log4j2 漏洞,太可怕了。。

        終于實(shí)現(xiàn)了 SpringBoot+WebSocket實(shí)時(shí)監(jiān)控異常....

        互聯(lián)網(wǎng)初中高級(jí)大廠面試題(9個(gè)G)

        內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper、數(shù)據(jù)結(jié)構(gòu)、限流熔斷降級(jí)......等技術(shù)棧!

        ?戳閱讀原文領(lǐng)?。?/span>? ? ? ? ? ? ? ??? ??? ? ? ? ? ? ? ? ? ?朕已閱?

        瀏覽 37
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国精产品综合视频 | 黄色小说文 | 男人天堂资源网 | 日韩一级免费视频 | 韩国美女视频黄是免费 | 中文字幕人妻丰满熟妇 | 大香蕉伊人网 | 黄色一级视频 | 国产精品嫩草AV城中村 | 国产精品白嫩大学美女 |