Spring 事件发布
2021-01-28 10:14
标签:owa 类型 this dap spring autowire inf mat type 基于观察者模式,主要方法为1 监听者注册 2 监听者注销 3 执行监听方法 MsgEvent:事件对象 MsgListener:事件监听 MsgListener2:事件监听(使用注解方式实现) MsgPublisher:事件发布器 SpringEventTest:单元测试类 MsgEvent MsgListener MsgListener2 MsgPublisher SpringEventTest listenter2 got message, ID:5938c2c4-0cda-4abc-b187-9d347102e867, payload:事件消息体xxx 流程简述: 1 注册Listener到容器中,集合存储 (本文忽略注册过程源码,着重发布事件和处理事件代码) 2 获取发布器SimpleApplicationEventMulticaster, 发布器在spring启动时会初始化 initApplicationEventMulticaster()方法 此处不细究 3 发布器根据事件source和事件类class从容器中获取监听器集合 4 遍历监听器集合, 并调用监听器EventListener的onApplication方法 以MsgPublisher为入口 跟踪方法 applicationContext.publishEvent(msgEvent) AbstractApplicationContext 持续追踪代码 SimpleApplicationEventMulticaster 发布器发布事件 Spring 事件发布 标签:owa 类型 this dap spring autowire inf mat type 原文地址:https://www.cnblogs.com/xieyanke/p/12837462.html1、设计模式
2、使用篇
类结构图
测试代码
@Data
public class MsgEvent extends ApplicationEvent{
//消息ID
private String msgId;
//消息体
private String payload;
public MsgEvent(Object source) {
super(source);
}
}
@Component
public class MsgListener implements ApplicationListener
@Component
public class MsgListener2 {
@EventListener
public void processMsg(MsgEvent event){
System.out.println("listenter2 got message, ID:"+event.getMsgId()+", payload:"+event.getPayload());
}
}
@Component
public class MsgPublisher implements ApplicationContextAware {
//持有当前容器
private ApplicationContext applicationContext;
//模拟业务触发事件
public void publish(){
MsgEvent msgEvent = new MsgEvent("这是一条消息事件");
msgEvent.setMsgId(UUID.randomUUID().toString());
msgEvent.setPayload("事件消息体xxx");
applicationContext.publishEvent(msgEvent);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
public class SpringEventTest extends BaseTest{
@Autowired
private MsgPublisher msgPublisher;
@Test
public void publish(){
msgPublisher.publish();
}
}
打印结果:
listenter1 got message, ID:5938c2c4-0cda-4abc-b187-9d347102e867, payload:事件消息体xxx
3、源码分析篇
@Component
public class MsgPublisher implements ApplicationContextAware {
//持有当前容器
private ApplicationContext applicationContext;
//模拟业务触发事件
public void publish(){
MsgEvent msgEvent = new MsgEvent("这是source");
msgEvent.setMsgId(UUID.randomUUID().toString());
msgEvent.setPayload("事件消息体xxx");
applicationContext.publishEvent(msgEvent);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
protected void publishEvent(Object event, ResolvableType eventType) {
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
}
// Decorate event as an ApplicationEvent if necessary
ApplicationEvent applicationEvent;
//若继承自ApplicationEvent 则直接转Application类型
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
//非继承自ApplicationEvent 则标准化成 PayloadApplicationEvent
//继承关系为 PayloadApplicationEvent
//广播事件
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
//事件类型
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
//事件类型筛选容器中已注册的监听器 并循环调用
for (final ApplicationListener> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
@Override
public void run() {
//调用监听器
invokeListener(listener, event);
}
});
}
else {
//调用监听器
invokeListener(listener, event);
}
}
}
//筛选获取监听器
protected Collection
上一篇:python-类的基本使用
下一篇:2020年最具“钱途”的编程语言