Change spring-servlet.xml Filename (Spring Web Contenxt Configuration Filename)

The first thing that we do when we want to implement Spring MVC in our project is to add DispatcherServlets entry in deployment descriptor (web.xml). Also we write a spring web configuration xxxx-servlet.xml which contains all the MVC mappings and data.

By default the name of file must be XXX-servlet.xml where XXX is the name of servlet. For example in below entry in Web.xml we defined servlet named “spring”.

<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Code language: HTML, XML (xml)

Note that the servlet name is “spring” and thus, Spring will by default load file named “spring-servlet.xml” from your webapps WEB-INF folder.

What if we want to load a file called “bean.xml” instead of default “XXX-servlet.xml” ?

Well, this can be achieved by passing one init-parameter to spring’s DispatcherServlet. Check the following web.xml snippet.

<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/bean.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Code language: HTML, XML (xml)

Note that in above code snippet, we have passed an init-param to DispatcherServlet called contextConfigLocation. Using this parameter not only can we change the name of Spring’s web context file but also change its location.

This parameter will call setContextConfigLocation method on DispatcherServlet and overrides default context config file. Note that it is possible to add multiple locations separated by any number of commas and spaced.

<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/bean.xml, /WEB-INF/bean-service.xml, /WEB-INF/bean-dao.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Code language: HTML, XML (xml)

I hope this is useful :)

View Comments

  • That's right. I found out that you can use the ContextLoaderListener-class to include multiple application also, e.g. like this

    contextConfigLocation

    classpath:applicationContext-persistence-relational.xml

    org.springframework.web.context.ContextLoaderListener

    Matt

  • Can you configure multiple init params in DispatcherServlet ?

    flexspring
    org.springframework.web.servlet.DispatcherServlet

    contextConfigLocation

    spring.profiles.active
    production

    1

  • i have problem like............ this above site is online now.........but in hierarchy....... /first.jsp and session.jsp set the session variable and /Temp/second.jsp get the session value............but if i place a session.jsp in main ROOT dir the it gives me value...............but in /Temp/session.jsp............it gives me NULL pointer exception..........................

    applcation.setAttribute("name1",name) // session.jsp
    application.getAttribute("name1") // /Temp/second.jsp.................. NULL...........................

    plz help me out......... thanks in advance

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago