SpringBoot事件监听

SpringBoot实现事件监听的4种方式

方式一

  1. 创建自定义事件类,继承自ApplicationEvent
package com.boot.event.eventdemo;

import org.springframework.context.ApplicationEvent;

public class MyApplicationEvent extends ApplicationEvent {

    public MyApplicationEvent(Object source) {
        super(source);
    }
}
  1. 创建自定义事件监听类,实现ApplicationListener接口
package com.boot.event.eventdemo;

import org.springframework.context.ApplicationListener;

public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

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

  1. 在启动类中添加事件并发布事件
package com.boot.event.eventdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(EventDemoApplication.class);

        // 方式1 添加事件
        application.addListeners(new MyApplicationListener());

        ConfigurableApplicationContext context = application.run(args);

        // 发布事件 自定义事件必须要发布,所以这一行不能够注释
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }

}

方式二

在方式一中,我们通过手动添加事件的方式来完成了自定义事件的添加,我们还可以直接通过将自定义事件监听交给Spring容器管理的方式,来完成自定义事件的添加,只需要将方式一中实现ApplicationListener接口的类添加上@Component这种注解就可以了,同时启动类中可以删除application.addListeners(new MyApplicationListener());这一行
代码如下

监听类

package com.boot.event.eventdemo;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

// 方式2,使用注解 @Component
public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

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

启动类

package com.boot.event.eventdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(EventDemoApplication.class);
        ConfigurableApplicationContext context = application.run(args);

        // 发布事件 自定义事件必须要发布,所以这一行不能够注释
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }

}

方式三

可以在配置文件中添加监听配置context.listener.classes=com.boot.event.eventdemo.MyApplicationListener,监听类删除@Component注解,启动类和方式二相同

方式四

利用注解@EventListener
添加新类,并交给Spring容器管理

package com.boot.event.eventdemo;

import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

// 方式4
@Component
public class HandlerEvent {

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

    @EventListener(ContextClosedEvent.class)
    public void handlerEvent(Object object) {
        System.out.println("接收到了事件=====" + object.getClass());
    }
}

启动类

package com.boot.event.eventdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class EventDemoApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(EventDemoApplication.class);
        ConfigurableApplicationContext context = application.run(args);

        // 发布事件 自定义事件必须要发布,所以这一行不能够注释
        context.publishEvent(new MyApplicationEvent(new Object()));

        context.close();
    }

}

可以注意到,在方式四中,添加了两个事件监听,但在启动类中,只发布了一个事件,这是因为事件ContextClosedEvent是Spring自带的事件,已经在启动的时候自动发布了,但自定义的事件依然需要手动发布。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值