为什么我的Spring ContextRefreshed事件被调用两次?

12 浏览
0 Comments

为什么我的Spring ContextRefreshed事件被调用两次?

我注册了一个Spring ApplicationListener bean来监听ContextRefreshed事件。然而,奇怪的是,当上下文初始化完成时,我会收到两次对onApplicationEvent(ContextRefreshedEvent)方法的调用。这是正常行为还是表示我的配置有问题?我在我的Servlet容器中使用Jetty 8。

我的相关web.xml配置如下:

contextConfigLocation

/WEB-INF/config/spring/spring-config.xml

Spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

1

org.springframework.web.context.ContextLoaderListener

Spring

/service/*

谢谢!

0
0 Comments

这个问题出现的原因是Spring框架的一个bug。如果使用的是Spring 3.0版本,建议尝试使用最新可用的版本,即3.0.5。然而,升级到3.0.5版本后问题仍然存在。甚至在3.1版本中也存在相同的问题。

解决方法:暂时还没有有效的解决方法。

0
0 Comments

原因:问题发生在一个事件监听器上,怀疑是ContextLoaderListener引起的。当从web.xml中移除该声明后,应用程序正常工作。随后想弄清楚ContextLoaderListener的目的。

解决方法:根据stackoverflow上的回答,ContextLoaderListener是可选的。可以在不配置ContextLoaderListener的情况下启动Spring应用程序,只需要最基本的web.xml和DispatcherServlet即可。

为什么我的Spring ContextRefreshed事件会被调用两次?

问题发生在一个事件监听器上,我怀疑是ContextLoaderListener引起的。当我从web.xml中移除该声明后,应用程序正常工作。然后我需要弄清楚ContextLoaderListener的目的。

根据stackoverflow上的回答,ContextLoaderListener是可选的。可以在不配置ContextLoaderListener的情况下启动Spring应用程序,只需要最基本的web.xml和DispatcherServlet即可。

0
0 Comments

Spring的ContextRefreshed事件在某些情况下会被调用两次的原因是,即使没有为DispatcherServlet指定contextConfigLocation,它仍然会创建一个子上下文,并且第二次刷新的事件是针对该上下文的。可以使用event.getApplicationContext()来确定事件所属的上下文。

解决方法之一是使用ApplicationContext的id或displayName属性来区分这两个事件。

另一种解决方法是,如果你有appContext(或实现了ApplicationContextAware接口),你可以将该appContext与事件中的上下文进行比较。

一个新手的问题是,如何从ContextRefreshedEvent中访问ID或displayName?

通过将ContextRefreshedEvent.getSource()转换为ApplicationContext,然后在该上下文上调用getDisplayName()或getId()方法,可以解决这个问题。

0