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

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

我的相关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/*  

谢谢!

即使您没有为DispatcherServlet指定contextConfigLocation,它仍会创建子上下文,而第二个刷新的事件也是针对该上下文的。 使用event.getApplicationContext()来查找事件的上下文。

它也发生在我身上,在一个不同的事件听众身上。 ( ApplicationListener

我怀疑ContextLoaderListener ,当我从web.xml中删除声明时,应用程序正常工作。 然后我必须弄清楚它的目的是什么,ContextLoaderListener ……

ContextLoaderListener在Spring中的角色/目的?

有趣的答案是:

ContextLoaderListener是可选的。 只是为了说明一点:你可以在没有配置ContextLoaderListener的情况下启动Spring应用程序…只是带有DispatcherServlet的基本最小web.xml

它看起来像虫子。

https://jira.springsource.org/browse/SPR-6589

如果您使用3.0,请在最新版本3.05上试用它。

我也有这个问题,但修好了。 我正在将dataSource注入到我的DAO中(并使用它实例化JdbcTemplate)….但我还为JDBCTemplate配置了一个Spring bean。

我本应该用jdbcTemplate注入我的DAO …避免重复。