我有一個Spring ApplicationListener bean註冊來偵聽ContextRefreshed事件。但由於某種奇怪的原因,我在上下文初始化完成後得到了兩個調用onApplicationEvent(ContextRefreshedEvent)
方法。這是正常行爲還是表示配置有問題?我爲我的Servlet容器使用Jetty 8。爲什麼我的Spring ContextRefreshed事件被調用兩次?
我相關的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>
謝謝!
就是這樣!現在我可以使用ApplicationContext的'id'或'displayName'屬性來區分這兩個事件。 – Andre 2011-05-29 13:06:00
或者,如果您@Autowire appContext(或實現ApplicationContextAware),您可以比較該appContext與事件中的那個。 – sourcedelica 2011-05-29 23:32:23
@Andre一個noob的問題:如何從ContextRefreshedEvent訪問ID或displayName? – 2015-11-27 14:39:36