名称为appServlet-servlet.xml的问题

时间:2013-08-20 15:53:26

标签: spring spring-mvc spring-security

大家好我正在使用Spring 3.2.3,hibernate 4.2.2和org.springframework.security 3.0.5。在我开始使用spring的安全性之前,我的上下文文件名为servlet-context.xml,一切正常。由于我在尝试运行应用程序时开始使用org.springframework.security 3.0.5,因此出现错误:

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/appServlet-servlet.xml]

我的web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<!-- Spring MVC -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
     </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<listener>
    <listener-class>
          org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/servlet-context.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<!-- Spring Security -->
<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>
  

请注意,我正在指定我的servlet-context.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/servlet-context.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

如果我像这样离开我的web.xml并将我的servlet-context.xml内容复制/粘贴到名为appServlet-servlet.xml的新文件中,一切正常。这让我感到困惑,因为我告诉web.xml我的上下文文件名是servlet-context.xml。我是否被迫将我的上下文文件称为appServlet-servlet.xml?当然,如果我删除了servlet-context.xml并让appServlet-servlet.xml在web.xml中进行了规范,那么工作正常。

如果我想在我的应用程序中使用spring security,我只想知道是否必须将我的上下文文件称为appServlet-servlet.xml。

1 个答案:

答案 0 :(得分:2)

这并不奇怪。 Spring以这种方式工作。由于您的DispatchServlet名称是appServlet,因此Spring会自动尝试查找具有相同名称的servlet上下文,此处为“appServlet-servlet.xml”。

以下是可能有用的页面:http://syntx.io/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/