Spring入门—内置事件、事件的发布监听、事件的处理机制以及监听器排序

读者福利

由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴

35K成功入职:蚂蚁金服面试Java后端经历!「含面试题+答案」

35K成功入职:蚂蚁金服面试Java后端经历!「含面试题+答案」

35K成功入职:蚂蚁金服面试Java后端经历!「含面试题+答案」

更多笔记分享

35K成功入职:蚂蚁金服面试Java后端经历!「含面试题+答案」

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

/**

  • 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 MyTestEvent(Object source) {

super(source);

}

}

这个event最好是继承了ApplicationEvent的子类,如果不是,那么Spring会把你的event包装到PayloadApplicationEvent中。

在这里插入图片描述

监听事件


监听事件需要实现ApplicationListener接口,最好在接口的泛型上写好事件的类型,不然所有的事件都会被监听到,最后,这个监听器要交给Spring管理。

@Component

public class MyTestEventListener implements ApplicationListener {

private static final Logger logger = LoggerFactory.getLogger(MyTestEventListener.class);

@Override

public void onApplicationEvent(MyTestEvent event) {

logger.info(“接收到事件[{}]了”,event);

}

}

当程序正常运行时,就会打印出内容了。

在这里插入图片描述

内置事件

====================================================================

Spring提供了5个标准事件,如下:

| 事件类型 | 说明 |

| :-: | :-- |

| ContextRefreshedEvent | 调用ConfigurableApplicationContext中的refresh时(refresh的最后会发布这个事件) |

| ContextStartedEvent | 调用ConfigurableApplicationContext中的start时被触发 |

| ContextStoppedEvent | 调用ConfigurableApplicationContext中的stop时被触发 |

| ContextClosedEvent | 调用ConfigurableApplicationContext中的close时被触发 |

| RequestHandledEvent | 在web应用中,当一个请求结束时触发该事件 |

事件处理机制

======================================================================

初始化广播者


首先是初始化广播者,初始化的逻辑是在这个方法中的

org.springframework.context.support.AbstractApplicationContext#initApplicationEventMulticaster

具体源码如下:

/**

  • Initialize the ApplicationEventMulticaster.

  • Uses SimpleApplicationEventMulticaster if none defined in the context.

  • @see org.springframework.context.event.SimpleApplicationEventMulticaster

*/

protected void initApplicationEventMulticaster() {

//先获取到beanFactory

ConfigurableListableBeanFactory beanFactory = getBeanFactory();

//看是否有自定义的广播者,注意,广播者的名字必须是APPLICATION_EVENT_MULTICASTER_BEAN_NAME

if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {

this.applicationEventMulticaster =

beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);

if (logger.isTraceEnabled()) {

logger.trace(“Using ApplicationEventMulticaster [” + this.applicationEventMulticaster + “]”);

}

}

else {

//如果没有,那就创建默认的广播者

this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);

beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);

if (logger.isTraceEnabled()) {

logger.trace(“No '” + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "’ bean, using " +

“[” + this.applicationEventMulticaster.getClass().getSimpleName() + “]”);

}

}

}

注册收集到的监听器


org.springframework.context.support.AbstractApplicationContext#registerListeners

/**

  • Add beans that implement ApplicationListener as listeners.

  • Doesn’t affect other listeners, which can be added without being beans.

*/

protected void registerListeners() {

// Register statically specified listeners first.

for (ApplicationListener<?> listener : getApplicationListeners()) {

getApplicationEventMulticaster().addApplicationListener(listener);

}

// Do not initialize FactoryBeans here: We need to leave all regular beans

// uninitialized to let post-processors apply to them!

//注意这里体现了所有的监听器都必须是ApplicationListener类型的

String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);

for (String listenerBeanName : listenerBeanNames) {

getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);

}

// Publish early application events now that we finally have a multicaster…

Set earlyEventsToProcess = this.earlyApplicationEvents;

this.earlyApplicationEvents = null;

if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {

for (ApplicationEvent earlyEvent : earlyEventsToProcess) {

getApplicationEventMulticaster().multicastEvent(earlyEvent);

}

}

}

面试题总结

其它面试题(springboot、mybatis、并发、java中高级面试总结等)

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

1715470732743)]

[外链图片转存中…(img-ubiYCjan-1715470732743)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 29
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值