为什么我的 Spring ContextRefreshed 事件被调用两次? [英] Why is my Spring ContextRefreshed event called twice?
问题描述
我注册了一个 Spring ApplicationListener bean 来监听 ContextRefreshed 事件.但出于某些奇怪的原因,我在上下文初始化完成时收到了对 onApplicationEvent(ContextRefreshedEvent)
方法的两次调用.这是正常行为还是表明我的配置有问题?我的 Servlet 容器使用 Jetty 8.
I have a Spring ApplicationListener bean registered to listen for ContextRefreshed events. For some odd reason though, I get two calls to the onApplicationEvent(ContextRefreshedEvent)
method at the completion of the context initialization. Is this normal behavior or is it indicative of a problem with my configuration? I'm using Jetty 8 for my Servlet container.
我的相关web.xml配置如下
My relevant web.xml configuration is as follows
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/spring-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
谢谢!
推荐答案
即使您没有为 DispatcherServlet 指定 contextConfigLocation,它仍然会创建一个子上下文,并且第二个刷新事件是针对该上下文的.使用 event.getApplicationContext() 找出事件针对的上下文.
Even though you did not specify a contextConfigLocation for your DispatcherServlet it still creates a child context and the second refreshed event is for that context. Use event.getApplicationContext() to find out which context the event is for.
这篇关于为什么我的 Spring ContextRefreshed 事件被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!