2012-07-10 118 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中。

相关问题