为什么我的Spring ContextRefreshed事件被调用两次?
问题内容:
我注册了一个Spring ApplicationListener
bean来监听ContextRefreshed事件。但是由于某种奇怪的原因,在上下文初始化完成时,我得到了对该方法的 两次
调用onApplicationEvent(ContextRefreshedEvent)
。这是正常现象,还是表明我的配置有问题?我正在为我的Servlet容器使用Jetty
8。
我相关的web.xml配置如下
<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()找出事件用于哪个上下文。