读者福利
由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴
更多笔记分享
/**
-
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中高级面试总结等)
1715470732743)]
[外链图片转存中…(img-ubiYCjan-1715470732743)]