title: Can you use a classpath file to configure DispatcherServlet with <init-param>?
body: <p>I was going through the Spring configuration files in a web application I’m working at and we have this in our <code>web.xml</code>:</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  </code></pre>
  
  <p>The reason why <code>applicationContext.xml</code> is loaded from the classpath, instead of the more usual <code>/WEB-INF/applicationContext.xml</code>, is because there are certain properties such as JDBC connection details that are filtered by Maven according to the profile used to build/run the app (integration testing, functional testing, production...). This all works, but if I change it to the configuration below (removing the <code>&lt;context-param&gt;</code> and <code>&lt;listener&gt;</code> entries):</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  </code></pre>
  
  <p>The logs say the application starts up correctly and the web application context is loaded from the XML file:</p>
  
  <pre><code>...
  11:57:15.910 [RMI TCP Connection(2)-127.0.0.1] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization started
  11:57:15.927 [RMI TCP Connection(2)-127.0.0.1] INFO  org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Aug 22 11:57:15 BST 2017]; root of context hierarchy
  11:57:15.958 [RMI TCP Connection(2)-127.0.0.1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
  ...
  </code></pre>
  
  <p>But when I try to get any page Tomcat replies with a 404 error and the following NPE in the logs (we’re using Tiles 3):</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet jsp threw exception
  java.lang.NullPointerException
      at org.apache.tiles.access.TilesAccess.getContainer(TilesAccess.java:124)
      at org.apache.tiles.access.TilesAccess.getContainer(TilesAccess.java:107)
      at org.apache.tiles.access.TilesAccess.getCurrentContainer(TilesAccess.java:174)
      at org.apache.tiles.template.InsertDefinitionModel.execute(InsertDefinitionModel.java:95)
      at org.apache.tiles.jsp.taglib.InsertDefinitionTag.doTag(InsertDefinitionTag.java:254)
      at ...
  </code></pre>
  
  <p>I’ve seen in other SO questions that you can configure your servlet using <code>&lt;init-param&gt;</code> if you don’t need to share the context among servlets (usually if you’re just running a single one); however all the examples I’ve seen use an absolute path to indicate the location of <code>applicationContext.xml</code>, so I was wondering that maybe it wasn’t possible to load the WAC using the classpath at all? Have you tried to do so successfully or do you know the reason why you can’t?</p>