2012-07-10 116 views
5

我有Spring和Spring安全性的web項目。 我的web.xml:爲什麼Spring Context加載兩次?

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     version="3.0" > 
     <display-name>BillBoard 
     </display-name> 
     <session-config> 
      <session-timeout> 
       30 
      </session-timeout> 
     </session-config> 
     <listener> 
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
     </listener> 
     <listener> 
      <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
     </listener> 
     <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value> 
     </context-param> 
     <servlet> 
      <servlet-name>billboard</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <init-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value> 
      </init-param> 
      <load-on-startup>1</load-on-startup> 

     </servlet> 
     <servlet-mapping> 
      <servlet-name>billboard</servlet-name> 
      <url-pattern>*.html</url-pattern> 
     </servlet-mapping> 
     <filter> 
      <filter-name>springSecurityFilterChain</filter-name> 
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
     </filter> 

     <filter-mapping> 
      <filter-name>springSecurityFilterChain</filter-name> 
      <url-pattern>/*</url-pattern> 
     </filter-mapping> 
    </web-app> 

在服務器日誌中我看到Spring上下文加載兩次(春豆初始化,數據庫createtion ...)。 DispatcherServlet首次執行此操作,並在第二次執行ContextLoaderListener時執行此操作。我該如何解決它?

this教程我看到,如果contextParam出現,那麼servlet init-params不是必需的。但如果我刪除init params我有錯誤:「org.apache.catalina.LifecycleException:org.apache.catalina.LifecycleException:java.io.FileNotFoundException:無法打開ServletContext資源[/WEB-INF/billboard-servlet.xml] 」。 Dispather servlet在默認位置查找上下文配置。

回答

2

這是兩個獨立的方法來做同樣的事情。例如,丟棄ContextLoaderListener

+0

如果我刪除contextLoaderListener,那麼我有異常「java.lang.IllegalStateException:沒有找到WebApplicationContext:沒有ContextLoaderListener註冊?」 – Balconsky 2012-07-10 08:20:11

+0

如果你放棄'contextConfigLocation'? – 2012-07-10 08:20:50

+0

我嘗試從DispatcherServlet中刪除上下文參數或init-param,但是這兩個選項都會引發錯誤。 – Balconsky 2012-07-10 08:33:35

6

你仍然需要爲你的servlet上下文:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

你不需要加載它作爲ContextLoaderListener雖然context-param

剛剛離開的security-config.xmlcontext-param(它有去那裏,因爲安全是每個應用程序的全球性),並作爲billboard-servlet.xml你的servlet的contextConfigLocation,它應該工作。

+0

對於用戶身份驗證數據庫,所以我在billboard-servlet.xml中定義了dataSource並在我的AuthenticationProvider中引用它的屬性。 – Balconsky 2012-07-10 09:01:27

+0

如果你的數據源在servlet外部使用,它不應該在'billboard-servlet.xml'中。把它放在你的根上下文中 - 無論如何,servlet會繼承它。 – soulcheck 2012-07-10 09:07:15

3

我有同樣的問題,究其原因是:

<load-on-startup>1</load-on-startup

+0

這導致上下文加載兩次。刪除它,沒有副作用。 – 2018-02-20 12:46:50

1

既然你有春天delegatingFilterProxy,如果你刪除contextLoaderLister你會得到下面的異常。

java.lang.IllegalStateException: No WebApplicationContext found: 
no ContextLoaderListener registered? 

所以通過調度的servlet加載通過contextLoaderLister安全-config.xml和廣告牌 - servlet.xml中。

相關問題