java 为什么我的 Spring ContextRefreshed 事件被调用两次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6164573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my Spring ContextRefreshed event called twice?
提问by Andre
I have a Spring ApplicationListener bean registered to listen for ContextRefreshed events. For some odd reason though, I get twocalls 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.
我注册了一个 Spring ApplicationListener bean 来监听 ContextRefreshed 事件。但出于某种奇怪的原因,我在上下文初始化完成时收到了两次对该onApplicationEvent(ContextRefreshedEvent)
方法的调用。这是正常行为还是表明我的配置有问题?我将 Jetty 8 用于我的 Servlet 容器。
My relevant web.xml configuration is as follows
我的相关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>
Thanks!
谢谢!
回答by sourcedelica
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.
即使您没有为 DispatcherServlet 指定 contextConfigLocation,它仍然会创建一个子上下文,并且第二个刷新事件是针对该上下文的。使用 event.getApplicationContext() 找出事件针对的上下文。
回答by OhadR
it happened to me as well, on a different event-listener. (ApplicationListener<AuthenticationFailureBadCredentialsEvent>
)
它也发生在我身上,在不同的事件监听器上。( ApplicationListener<AuthenticationFailureBadCredentialsEvent>
)
I suspected the ContextLoaderListener, and when I removed the declaration from the web.xml, the app was working properly. Then I had to figure out what is its purpose, of the ContextLoaderListener...
我怀疑ContextLoaderListener,当我从 web.xml 中删除声明时,应用程序运行正常。然后我必须弄清楚 ContextLoaderListener 的目的是什么......
Role/Purpose of ContextLoaderListener in Spring?
Spring 中 ContextLoaderListener 的作用/目的?
the interesting answer there is:
有趣的答案是:
ContextLoaderListener is optional. Just to make a point here: you can boot up a Spring application without ever configuring ContextLoaderListener ...just the basic minimum web.xml with DispatcherServlet
ContextLoaderListener 是可选的。只是在这里说明一点:您可以在不配置 ContextLoaderListener 的情况下启动 Spring 应用程序......只是基本的最小 web.xml 和 DispatcherServlet
回答by danny.lesnik
It looks like bug.
它看起来像错误。
https://jira.springsource.org/browse/SPR-6589
https://jira.springsource.org/browse/SPR-6589
If you are using 3.0 try it on the latest available release which is 3.05.
如果您使用的是 3.0,请在最新的可用版本 3.05 上试用。
回答by Kev
I had this problem too but fixed it. I was injecting the dataSource into my DAO (and instantiating a JdbcTemplate with it)....but I also had a Spring bean configured for JDBCTemplate.
我也有这个问题,但解决了。我正在将数据源注入到我的 DAO(并用它实例化一个 JdbcTemplate)......但我也有一个为 JDBCTemplate 配置的 Spring bean。
I should have been injecting my DAO with the jdbcTemplate...that avoids the duplicate.
我应该用 jdbcTemplate 注入我的 DAO ......避免重复。