【Spring Boot架构】自定义事件及监听

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/sinat_27933301/article/details/101628428

  这里的自定义事件及监听,其实早在Spring框架就有完善的事件监听机制。Spring的事件为Bean与Bean之间的消息通信提供了支持。当一个Bean处理完任务后,希望另一个Bean知道并能做相应的处理,这时就需要让另一个Bean监听当前Bean的所发送的事件。

  Spring框架中实现监听事件的流程
(1)自定义事件,继承ApplicationEvent抽象类
(2)定义事件监听器,实现ApplicationListener接口
(3)使用容器中发布事件

示例一

1、自定义事件

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}

2、定义事件监听器

public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

    @Override
    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接受到了事件:"+event.getClass());
        System.out.println("接受到了事件:"+event.getSource());
    }
}

3、使用容器中发布事件

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EventDemoApplication.class);
        //1 添加监听事件
        app.addListeners(new MyApplicationListener());
        ConfigurableApplicationContext context = app.run(args);
        // 发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }
}

控制台输出:

接受到了事件:class com.boot.event.eventdemo.MyApplicationEvent
接受到了事件:java.lang.Object@f713686

示例二(注解式,最常用)

1、自定义事件

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}

2、@EventListener注解的方式监听

@Component
public class HandlerEvent {

    @EventListener(MyApplicationEvent.class)
    public void handlerEvent(MyApplicationEvent event) {
        System.out.println("接受到了事件====:"+event.getClass());
        System.out.println("接受到了事件====:"+event.getSource());
    }
}

3、使用容器中发布事件

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EventDemoApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        // 发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }
}

控制台输出:

接受到了事件====:class com.boot.event.eventdemo.MyApplicationEvent
接受到了事件====:java.lang.Object@352c308

示例三(配置文件)

1、自定义事件

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}

2、定义事件监听器

public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

    @Override
    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接受到了事件:"+event.getClass());
        System.out.println("接受到了事件:"+event.getSource());
    }
}

3、使用容器中发布事件

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(EventDemoApplication.class);
        ConfigurableApplicationContext context = app.run(args);
        // 发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }
}

4、application.properties中配置

context.listener.classes=com.boot.event.eventdemo.MyApplicationListener

5、控制台输出

接受到了事件:class com.boot.event.eventdemo.MyApplicationEvent
接受到了事件:java.lang.Object@3a0807b7

猜你喜欢

转载自blog.csdn.net/sinat_27933301/article/details/101628428