SpringBoot实现事件监听的4种方式
方式一
- 创建自定义事件类,继承自
ApplicationEvent
package com.boot.event.eventdemo;
import org.springframework.context.ApplicationEvent;
public class MyApplicationEvent extends ApplicationEvent {
public MyApplicationEvent(Object source) {
super(source);
}
}
- 创建自定义事件监听类,实现
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());
}
}
- 在启动类中添加事件并发布事件
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自带的事件,已经在启动的时候自动发布了,但自定义的事件依然需要手动发布。