Spring MVC – Change default Spring-Dispatcher-Servlet.xml to user-defined context filename in web.xml

In this article, we will quickly go through in understanding filename and its location of Spring-Dispatcher-Servlet in Spring MVC

  1. First we will understand, default configuration of the DispatcherServlet in Spring MVC
  2. Later will try to modify the filename and its location of the DispatcherServlet to user-defined in accordance with the project requirements

1. Default Spring-Dispatcher-Servlet in Spring MVC (in web.xml)

  • By default, the name of the dispatcher servlet is XXX-servlet.xml where XXX is the servlet name
  • In the below example, name of the servlet is ‘mvc-dispatcher
  • And in this case, Spring container will by default loads the file named ‘mvc-dispatcher-servlet.xml’ from the location ‘/WEB-INF/

Q) What if user/projects require having different filename in accordance with architecture and storing it in the different classpath location ?

Answer: Move to the explanation 2

web.xml

<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered controllers -->
	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- location of the root application context xml file -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
	</context-param>

2. User-defined filename & location instead of default filename (in web.xml)

  • If you look at the below web.xml file, servlet name defined is ‘mvc-dispatcher’ but name of the file and its location are different from the default
  • For example in the below web.xml file, filename and its location which are defined in the contextConfigLocation parameter under <context-param> element is ‘WEB-INF/config/mvc-rest-dispatcher.xml’
  • This is different from the default filename that Spring container looks for while loading

web.xml

<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered controllers -->
	<servlet>
		<servlet-name>mvc-rest-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-rest-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- location of the root application context xml file -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/mvc-rest-dispatcher.xml</param-value>
	</context-param>

After deploying war into the Tomcat application server, spring container loads the servlet context resource from the context-param defined in the web.xml

Look at the below console, its loading the context resource i.e.; spring-dispatcher-servlet from the location ‘/WEB-INF/config/’ with filename ‘mvc-rest-dispatcher.xml’ appended to it. (/WEB-INF/config/ mvc-rest-dispatcher.xml)

dispatcher-servlet-filename

3. Wait, something more to explain

Q) What if project requires loading more than one context resources from different classpath location ?

  • Well, in that case you can simply add the complete classpath location of the files with comma-separated in the contextConfigLocation under <context-param> element
  • In the below example, we have got three files namely,
    • WEB-INF/config/network.xml
    • WEB-INF/customer.xml
    • WEB-INF/config/inventory.xml

web.xml

<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered controllers -->
	<servlet>
		<servlet-name>mvc-rest-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-rest-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- location of the root application context xml file -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/network.xml, /WEB-INF/customer.xml, /WEB-INF/config/inventory.xml</param-value>
	</context-param>

Conclusion:

  • Spring provides the capability to change the filename/location by overriding the default configuration
  • And this is pretty helpful depending on the project requirements and complying with the project architecture

Related Articles:

Happy Coding !!
Happy Learning !!

Spring MVC - Interview question on ModelAndView and DispatcherServlet
Spring MVC 4.0 Restful web service using @RestController annotation