0
votes

For some reasons, I need to name the XML files for the Spring Context in a way different from the standard.

Yes, I know that this is a basic question and there are several responses to it on Stack Overflow, for example:

However, my question is a little bit trickier than those, because the system only runs if the web.xml file has BOTH <init-param> and <context-param> doing THE SAME THING, as showed in the code below:

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

AND

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

If I eliminate only the TAG A: <init-param> the system doesn't run and the error is:

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

(the name of the servlet is "my-dispatcher")

and if I eliminate only the TAG B: <context-param> the system doesn't run either and the error is:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

If I don't eliminate any of them the system runs normally. Here is the web.xml that works:

<?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>

Does anyone have a simple(?) explanation for this behavior?

Thanks in advance.

Almir Campos.

1

1 Answers

3
votes

There are two things here:

  • The root ApplicationContext, which is loaded by ContextLoaderListener, which by defaults looks for applicationContext.xml file to load the context.

  • The ServletContext for each DispatcherServlet in your application, which is the child of the root context. The configuration file for each dispatcher servlet is by default in the pattern <servletname>-servlet.xml. Since the name of the servlet here is my-dispatcher, the DispatcherServlet needs a my-dispatcher-servlet.xml in classpath.

Now, if you don't have either of those files, and have different file names for either of those contexts, you have to configure them in the web.xml. That you do with contextConfigLocation parameter inside the <context-param> tag for root ApplicationContext and <init-param> tag for the DispatcherServlet.

So, if you remove any of them, default files for the respective context will be searched for, which you don't have, and hence you see those errors.

See also: