由于某些原因,我需要以与标准不同的方式命名 Spring Context 的 XML 文件。
是的,我知道这是一个基本问题,Stack Overflow 上有几个对此问题的回答,例如:
- Spring Context Configuration
- Spring MVC Context Hiearchy
- Spring-MVC: What are a "context" and "namespace"?
但是,我的问题比这些问题要棘手一些,因为系统仅在 web.xml 文件同时具有
<!--
TAG A: <init-param>
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myDearServlet.xml,
/WEB-INF/springSecurityStuff.xml
</param-value>
</init-param>
并且
<!--
TAG B: <context-param>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myDearServlet.xml,
/WEB-INF/springSecurityStuff.xml
</param-value>
</context-param>
如果我仅删除TAG A:
block 引用>java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/my-dispatcher-servlet.xml]
(the name of the servlet is "my-dispatcher")如果我只删除 TAG B:
系统也不会运行,错误是: block 引用>Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
如果我不消除其中任何一个,系统就会正常运行。这是有效的 web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd" version="2.4"> <display-name>Weird Spring MVC</display-name> <!-- SPRING MVC SERVLET --> <servlet> <servlet-name>my-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- TAG A: <init-param> --> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/myDearServlet.xml, /WEB-INF/springSecurityStuff.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>my-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- TAG B: <context-param> --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/myDearServlet.xml, /WEB-INF/springSecurityStuff.xml </param-value> </context-param> <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>
有人对这种行为有简单的(?)解释吗?
提前致谢。
阿尔米尔·坎波斯。
最佳答案
这里有两件事:
根
ApplicationContext
,由ContextLoaderListener
加载,默认情况下查找applicationContext.xml
文件来加载上下文。ServletContext
对于每个DispatcherServlet
在您的应用程序中,它是根上下文的子级。每个调度程序 servlet 的配置文件默认采用<servletname>-servlet.xml
模式。 。由于这里的servlet的名称是my-dispatcher
,DispatcherServlet
需要my-dispatcher-servlet.xml
在类路径中。
现在,如果您没有这些文件中的任何一个,并且对于这些上下文中的任何一个有不同的文件名,则必须在 web.xml
中配置它们。 。你用 contextConfigLocation
做的事<context-param>
内的参数root 的标签 ApplicationContext
和<init-param>
标签 DispatcherServlet
.
因此,如果您删除其中任何一个,系统将搜索相应上下文的默认文件,而您没有这些文件,因此您会看到这些错误。
另请参阅:
关于java - 奇怪的 Spring MVC 上下文配置行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360967/