欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot 事件監(jiān)聽的實(shí)現(xiàn)方法

 更新時(shí)間:2019年04月12日 08:25:27   作者:lijingyulee  
這篇文章主要介紹了springboot 事件監(jiān)聽的實(shí)現(xiàn)方法,并詳細(xì)的介紹了四種監(jiān)聽方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

定義事件

@Getter
public class TestEvent extends ApplicationEvent {
 private String msg;

 public TestEvent(Object source, String msg) {
  super(source);
  this.msg = msg;
 }
}

定義事件監(jiān)聽(注解方式)

 @Component
 public class TestListen {
 @EventListener
 public void testListen(TestEvent event) {
  System.out.println(event.getMsg());
 }
}

注意:@Component 注解

發(fā)布事件

@Autowired
private ApplicationContext publiser;

@GetMapping("test-listen")
public void testListen() {
 for (int i = 0; i < 10; i++) {
  System.out.println("i = " + i);
 }
 publiser.publishEvent(new TestEvent(this, "測試事件監(jiān)聽"));
 for (int j = 0; j < 10; j++) {
  System.out.println("j = " + j);
 }
}

測試時(shí)執(zhí)行順序:

  • i循環(huán)
  • 打印"event = [測試事件監(jiān)聽]"
  • j循環(huán)

異步監(jiān)聽

監(jiān)聽加上@Async注解

@Component
public class TestListen {
 @EventListener
 @Async
 public void testListen(TestEvent event) {
  for (int i = 0; i < 10; i++) {
   System.out.println("event = [" + event.getMsg() + "]");
  }
 }
}

測試時(shí)執(zhí)行順序:

  • i循環(huán)
  • j循環(huán)
  • 打印"event = [測試事件監(jiān)聽]"

代碼: async

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<T>接口,然后實(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)行測試(在啟動(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("測試事件."));
 }
}

啟動(dòng)后,日志打印如下:

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)

聽到事件源:測試事件..

由日志打印可以看出,SpringBoot四種事件的實(shí)現(xiàn)方式監(jiān)聽是有序的

完整的代碼路徑:https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis中使用CASE?WHEN關(guān)鍵字報(bào)錯(cuò)及解決

    mybatis中使用CASE?WHEN關(guān)鍵字報(bào)錯(cuò)及解決

    這篇文章主要介紹了mybatis中使用CASE?WHEN關(guān)鍵字報(bào)錯(cuò)及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)

    java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)

    這篇文章主要介紹了java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解Mybatis中的CRUD

    詳解Mybatis中的CRUD

    這篇文章主要介紹了Mybatis中的CRUD的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • springboot整合minio實(shí)現(xiàn)文件存儲(chǔ)功能

    springboot整合minio實(shí)現(xiàn)文件存儲(chǔ)功能

    MinIO?是一個(gè)基于Apache?License?v2.0開源協(xié)議的對(duì)象存儲(chǔ)服務(wù),它兼容亞馬遜S3云存儲(chǔ)服務(wù)接口,非常適合于存儲(chǔ)大容量非結(jié)構(gòu)化的數(shù)據(jù),本文給大家介紹了springboot整合minio實(shí)現(xiàn)文件存儲(chǔ)功能,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 一問詳解SpringBoot配置文件優(yōu)先級(jí)

    一問詳解SpringBoot配置文件優(yōu)先級(jí)

    在SpringBoot項(xiàng)目當(dāng)中,我們要想配置一個(gè)屬性,可以通過這三種方式當(dāng)中的任意一種來配置都可以,那么優(yōu)先級(jí)怎么算,本文主要介紹了一問詳解SpringBoot配置文件優(yōu)先級(jí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • java web服務(wù)器實(shí)現(xiàn)跨域訪問

    java web服務(wù)器實(shí)現(xiàn)跨域訪問

    這篇文章主要為大家詳細(xì)介紹了java web服務(wù)器實(shí)現(xiàn)跨域訪問,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • IDEA提示:Boolean method ‘xxx‘ is always inverted問題

    IDEA提示:Boolean method ‘xxx‘ is always&nb

    這篇文章主要介紹了IDEA提示:Boolean method ‘xxx‘ is always inverted問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java 中clone()的使用方法

    java 中clone()的使用方法

    這篇文章主要介紹了java 中clone()的使用方法的相關(guān)資料,希望通過本文能幫助大家能掌握clone()的克隆方法,需要的朋友可以參考下
    2017-09-09
  • JAVA中的Configuration類詳解

    JAVA中的Configuration類詳解

    這篇文章主要介紹了JAVA中的Configuration類詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • idea報(bào)錯(cuò):程序包org.springframework.web.bind.annotation不存在

    idea報(bào)錯(cuò):程序包org.springframework.web.bind.annotation不存在

    在用本地的maven倉庫的時(shí)候會(huì)org.springframework.web.bind.annotation不存在的錯(cuò)誤,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解下
    2023-08-08

最新評(píng)論