zoukankan      html  css  js  c++  java
  • SpringBoot的事件监听

    事件监听的流程分为三步:
    1、自定义事件,一般是继承ApplicationEvent抽象类。
    2、定义事件监听器,一般是实现ApplicationListener接口。
    3、a、启动的时候,需要将监听器加入到Spring容器中。

       b、或者将监听器加入到容器中。@Component

       c、使用@EventListener注解,在方法上面加入@EventListener注解,且该类需要纳入到spring容器中进行管理。 

       d、或者使用配置项,在默认的配置文件application.properties配置文件里面加入进去,context.listener.classes配置项。context.listener.classes=com.bie.license.ListenerApplicationListener
    4、发布事件。使用ApplicationContext.publishEvent发布事件。

    1、事件监听第一步,定义一个事件,继承ApplicationEvent抽象类。

     1 package com.bie.license;
     2 
     3 import org.springframework.context.ApplicationEvent;
     4 
     5 /**
     6  * 
     7  * @Description TODO
     8  * @author biehl
     9  * @Date 2018年12月31日 下午5:02:43
    10  * 1、第一步,创建一个事件,继承ApplicationEvent
    11  *  定义事件
    12  */
    13 
    14 public class EventApplicationEvent extends ApplicationEvent{
    15 
    16     /**
    17      * 
    18      */
    19     private static final long serialVersionUID = 1L;
    20 
    21     public EventApplicationEvent(Object source) {
    22         super(source);
    23     }
    24 
    25 }

    2、第二步,定义一个监听器,看看是监听那个事件。继承ApplicationListener类。

     1 package com.bie.license;
     2 
     3 import org.springframework.context.ApplicationListener;
     4 
     5 /**
     6  * 
     7  * @Description TODO
     8  * @author biehl
     9  * @Date 2018年12月31日 下午5:05:46
    10  * 2、第二步,定义一个监听器,监听哪一个事件。如果不执行第三步,将ListenerApplicationListener加入到容器中,使用@Component注解也可以的。
    11  */
    12 
    13 public class ListenerApplicationListener implements ApplicationListener<EventApplicationEvent>{
    14 
    15     @Override
    16     public void onApplicationEvent(EventApplicationEvent event) {
    17         System.out.println("接受到事件 : " + event.getClass());
    18     }
    19 
    20 }

    3、第三步,启动的时候,需要将监听器加入到Spring容器中。发布事件。使用ApplicationContext.publishEvent发布事件。

     1 package com.bie.license;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.context.ConfigurableApplicationContext;
     6 
     7 /**
     8  * 
     9  * @Description TODO
    10  * @author biehl
    11  * @Date 2018年12月31日 下午5:09:10
    12  *
    13  */
    14 @SpringBootApplication
    15 public class ListenerApplication {
    16 
    17     public static void main(String[] args) {
    18         SpringApplication app = new SpringApplication(ListenerApplication.class);
    19         app.addListeners(new ListenerApplicationListener());//app.addListeners(new ListenerApplicationListener());或者将ListenerApplicationListener加入到bean中也可以。
    20         ConfigurableApplicationContext context = app.run(args);
    21         // 第三步,发布事件
    22         context.publishEvent(new EventApplicationEvent(new Object()));
    23         // 关闭
    24         context.close();
    25     }
    26 }

    运行效果如下所示:

    使用@EventListener注解来进行加入到Spring容器中:

     1 package com.bie.license;
     2 
     3 import org.springframework.context.ApplicationEvent;
     4 import org.springframework.context.event.EventListener;
     5 import org.springframework.stereotype.Component;
     6 
     7 /**
     8  * 
     9  * @Description TODO
    10  * @author biehl
    11  * @Date 2018年12月31日 下午5:38:10
    12  *
    13  */
    14 @Component
    15 public class EventHandle {
    16 
    17     /**
    18      * 参数任意
    19      */
    20     @EventListener
    21     public void event(ApplicationEvent event) {
    22         System.out.println("EventHandle 接受到事件 : "  + event.getClass());
    23     }
    24 }

    待续.......

  • 相关阅读:
    【转】基于CNN目标检测方法(RCNN,Fast-RCNN,Faster-RCNN,Mask-RCNN,YOLO,SSD)行人检测,目标追踪,卷积神经网络
    YOLOv1_paper
    程序的编译链接过程【转】
    tensorflow-MTCNN【转】
    FaceNet实现人脸检测和识别
    ORACLE常用SQL优化hint语句
    SQL Cursor生命周期
    Hive SQL的编译过程
    深入浅出数据仓库中SQL性能优化之Hive篇
    Hive基础
  • 原文地址:https://www.cnblogs.com/biehongli/p/10202541.html
Copyright © 2011-2022 走看看