为什么我的Spring ContextRefreshed事件被调用了两次? [英] Why is my Spring ContextRefreshed event called twice?

查看:1033
本文介绍了为什么我的Spring ContextRefreshed事件被调用了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注册了一个Spring ApplicationListener bean来监听ContextRefreshed事件。但是出于一些奇怪的原因,我在上下文初始化完成时得到两个 onApplicationEvent(ContextRefreshedEvent)方法的调用。这是正常行为还是表示我的配置有问题?我正在使用Jetty 8作为我的Servlet容器。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆