[英]Why is my Spring ContextRefreshed event called twice?
我注冊了一個Spring ApplicationListener bean來監聽ContextRefreshed事件。 但是由於一些奇怪的原因,我在完成上下文初始化時onApplicationEvent(ContextRefreshedEvent)
方法進行兩次調用。 這是正常行為還是表示我的配置有問題? 我正在使用Jetty 8作為我的Servlet容器。
我的相關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()來查找事件的上下文。
它發生在我身上,也發生在一個不同的事件聽眾身上。 ( ApplicationListener<AuthenticationFailureBadCredentialsEvent>
)
我懷疑ContextLoaderListener ,當我從web.xml中刪除聲明時,應用程序正常工作。 然后我必須弄清楚它的目的是什么,ContextLoaderListener ......
ContextLoaderListener在Spring中的角色/目的?
有趣的答案是:
ContextLoaderListener是可選的。 只是為了說明一點:你可以在沒有配置ContextLoaderListener的情況下啟動Spring應用程序...只是帶有DispatcherServlet的基本最小web.xml
我也有這個問題,但修好了。 我正在將dataSource注入到我的DAO中(並使用它實例化JdbcTemplate)....但我也為JDBCTemplate配置了一個Spring bean。
我本應該用jdbcTemplate注入我的DAO ...避免重復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.