在一个Spring容器中,我们可以发送消息给Spring容器,容器收到消息后自动接受消息并进行处理
事件监听主要有两部分组成,一个是事件源,另一个是监听事件并处理的对象
使用的时候,我们只需要自定义事件继承事件对象ApplicationEvent,自定义监听器实现监听器ApplicationListener,并将监听器注册到Spring容器中即可
Spring事件对象为ApplicationEvent,继承自ObjectEvent
public abstract class ApplicationEvent extends EventObject {
/**
* Create a new ApplicationEvent.
* 创建一个新的应用事件对象
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
}
当事件对象被发布到Spring中的监听器,将会找出对应的监听器对事件进行处理
Spring中的事件监听器为ApplicationListener,继承自EventListener
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E var1);
}
创建事件对象
public class MyApplicationEvent extends ApplicationEvent {
public MyApplicationEvent(Object source) {
super(source);
}
}
创建监听器
public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {
@Override
public void onApplicationEvent(MyApplicationEvent event) {
System.out.println("收到事件:" + event);
}
}
注册监听器
public class ApplicationEventBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
// 注册自定义事件监听器
context.addApplicationListener(new MyApplicationListener());
// 启动上下文
context.refresh();
// 发布事件
context.publishEvent(new MyApplicationEvent(context));
// 结束
context.close();
}
}
举例:浙商资产管理中预警事件
Event
public class WarningEvent extends ApplicationEvent {
private Map<String,String> content;
/**
* Create a new {@code ApplicationEvent}.
*
* @param source the object on which the event initially occurred or with
* which the event is associated (never {@code null})
*/
public WarningEvent(Object source) {
super(source);
}
public WarningEvent(Object source, Map<String,String> content) {
super(source);
this.content = content;
}
public Map<String,String> getContent() {
return content;
}
public void setContent( Map<String,String> content) {
this.content = content;
}
}
Listener
@Slf4j
@Component // 将监听器交给Spring容器管理
public class WarningListener implements ApplicationListener<WarningEvent> {
@Override
public void onApplicationEvent(WarningEvent event) {
// do something
}
}
Test:伪代码,重点在于使用WebApplicationContext容器发布消息
@Component
public class TestEvent{
@Autowired
private WebApplicationContext webApplicationContext;
Map<String,String> params3 = new HashMap<>();
params3.put("deviceName",deviceName);
params3.put("deviceSerial",deviceSerial);
params3.put("url",picUrl);
params2.put("url",picUrl);
WarningEvent event = new WarningEvent("object",params3);
webApplicationContext.publishEvent(event);
}
实现方式:
事件对象
public class WarningEvent extends ApplicationEvent {
private Map<String,String> content;
/**
* Create a new {@code ApplicationEvent}.
*
* @param source the object on which the event initially occurred or with
* which the event is associated (never {@code null})
*/
public WarningEvent(Object source) {
super(source);
}
public WarningEvent(Object source, Map<String,String> content) {
super(source);
this.content = content;
}
public Map<String,String> getContent() {
return content;
}
public void setContent( Map<String,String> content) {
this.content = content;
}
}
注册监听器
@Component // 也要把该类交给Spring容器管理
public class ListenerConfig{
@Listener(WarningEvent.class) // 标明监听的哪个事件
public void onEvent(WarnEvent event){
Object source = event.getSouce();
// do something
}
}
发布事件
@Component
public class TestEvent{
@Autowired
private WebApplicationContext webApplicationContext;
Map<String,String> params3 = new HashMap<>();
params3.put("deviceName",deviceName);
params3.put("deviceSerial",deviceSerial);
params3.put("url",picUrl);
params2.put("url",picUrl);
WarningEvent event = new WarningEvent("object",params3);
webApplicationContext.publishEvent(event);
}
public class MyListener1 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener1.class);
public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s监听到事件源:%s.", MyListener1.class.getName(), event.getSource()));
}
}
@SpringBootApplication
public class LisenterApplication
{
public static void main(String[] args)
{
ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
//装载监听,完成
context.addApplicationListener(new MyListener1());
}
}