为什么Spring Context被加载两次? [英] Why Spring Context is loaded twice?
问题描述
我有一个带有Spring和Spring安全性的Web项目. 我的web.xml:
I have web project with Spring and Spring security. My web.xml:
<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上下文被加载了两次(Spring Bean初始化,数据库创建...).第一次使用DispatcherServlet进行此操作,第二次使用ContextLoaderListener.我该如何解决?
In server logs I see Spring context is loaded twice (spring bean initialization, database createtion...). At first time DispatcherServlet does it, and at the secont time ContextLoaderListener. How can I fix it?
在此教程中,我看到如果提供了contextParam,那么servlet不需要init-params.但是,如果我删除了初始化参数,则会出现错误:"org.apache.catalina.LifecycleException:org.apache.catalina.LifecycleException:java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/billboard-servlet.xml] ". Dispather servlet在默认位置找到上下文配置.
In this tutorial I see that if contextParam is presented then servlet init-params are not required. But if I remove init params I have error: "org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/billboard-servlet.xml]". Dispather servlet finds context-configuration in default location.
推荐答案
您仍然需要servlet上下文:
You still need a context for your servlet:
在初始化DispatcherServlet时,Spring MVC在Web应用程序的WEB-INF目录中查找名为[servlet-name] -servlet.xml的文件,并创建在那里定义的bean,从而覆盖任何使用以下命令定义的bean的定义.在全球范围内具有相同的名称.
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
.
You don't need to load it as context-param
in the ContextLoaderListener
though.
只需将security-config.xml
保留为context-param
(因为每个应用程序的安全性是全局的),就必须将security-config.xml
保留在那里,而将billboard-servlet.xml
保留为servlet的contextConfigLocation
,它应该可以工作.
Just leave the security-config.xml
as context-param
(it has to go there, as security is global per application), and billboard-servlet.xml
as contextConfigLocation
of your servlet and it should work.
这篇关于为什么Spring Context被加载两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!