我认为类 AbstractApplicationContext 的方法 publishEvent 中存在错误:
public void publishEvent(ApplicationEvent event) {
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
}
getApplicationEventMulticaster().multicastEvent(event);
if (this.parent != null) {
this.parent.publishEvent(event);
}
}
在 MVC 应用程序中有一个 spring-context-hierarchy、root-context 和 child-context。如果我刷新子上下文,那么这个方法也会刷新父上下文。但它不能使用相同的 ApplicationEvent-object 执行此操作,因为它包含引用上下文的源属性,该属性已被刷新:
/**
* Create a new ApplicationEvent.
* @param source the component that published the event (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
所以,我认为这是错误的,如果父(根)上下文发送一个事件并说,该子上下文已刷新。例如,有一个类 SourceFilteringListener 使用此属性来决定是否应该传递事件:
public void onApplicationEvent(ApplicationEvent event) {
if (event.getSource() == this.source) {
onApplicationEventInternal(event);
}
}
我的假设是否正确(这是一个错误)???我这么认为的原因是我调查了我的 MVC 应用程序中的一个错误,我试图找出为什么某些组件没有收到刷新事件。