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

spring-logo 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>

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>

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>

I hope this is useful :)

Get our Articles via Email. Enter your email address.

You may also like...

22 Comments

  1. james_coder says:

    its very good to know this new feature of spring …
    is it available in all spring version ?

  2. animesh says:

    thankx i was searching for this

  3. matzlo says:

    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

  4. Nazia says:

    Not very helpful 2 me…
    the data is not there whatever i m looking 4.

  5. Jkat says:

    Can you configure multiple init params in DispatcherServlet ?

    flexspring
    org.springframework.web.servlet.DispatcherServlet

    contextConfigLocation

    spring.profiles.active
    production

    1

  6. viveka says:

    Very good explanation ever seen this information thank you very much

  7. jyotiK says:

    Good and basic thing one should know to start with Spring.

  8. Sasi says:

    Yeah.. Its very useful….and.basic thing one should know to start with Spring.

  9. Vijaykumar says:

    Excellent tutorial. Worked fine.

  10. Ishwar Malvi says:

    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

  11. Anoop says:

    I’ve many services in their own respective packages. For each service there is a specific Spring based configuration, which I want to place in the very packages in which the services reside.

    Lets say addService, subtractService are my 2 services. I want addConfig.xml and subtractConfig.xml to be loaded while starting up in a Dynamic Web Project. All the solutions ask me to place my *.xml files in WEB-INF, but I don’t want this. Is there is a solution for this?

    I specify WEB-INF/*Config.xml in web.xml in , but that will only look in WEB-INF package, right?

  12. Andrea Nobili says:

    Tnx very much, this tutorial solve a mbig doubt looking at the Spring MVC Showcase tutorial

  13. Maribel says:

    I actually Think that article, “Change spring-servlet.xml filename.
    Spring context config location” Discount Roman Shades
    was really good! I actuallycould not agree together with u
    more! Finally looks like I reallystumbled upon a site definitely worth browsing.
    Thanks a lot, Niklas

  14. hild says:

    Can i specify contextConfigLocation value using classpath ? I want to keep the bean.xml (from your example) file some where else than WEB-INF and want to include the folder location in my classpath. Is there way we can achieve that ?
    Something like

    <param-value>classpath:config/dispatcher-servlet.xml</param-value> 
  15. Jamal says:

    Thx a lot, Excellent tutorial !!!

  16. TeaOudom says:

    Hello !! I’m a beginning of spring frame work so I was try to following on this tutorials above but not work so can you explain more how to configure web.XML file. any way as you mention above the jsp folder should under the WEBINF folder but it’s has a problem.
    please reply back ….. big thank for seeing this comment.

  17. Vasant Talpada says:

    …or if file name pattern is same then in above case we can write /WEB-INF/bean*.xml for all three files

  18. deepak says:

    Thank you so much, good job keep it up.

  19. Sanjeev says:

    Nice work and easily explained

  20. gaurav dasgupta says:

    Wow that was cool. I was stuck in a situation. I have 2 copies of XXXX-servlet.xml in my application. One is under WEB-INF and other is under src. Issue was that first time when the application loads it reads the XXXX-servlet.xml from WEB-INF but for the rest of the views rendered it read from XXXX-servlet.xml kept under src folder. After reading this solution of yours now I can atleast change the names of these 2 files.

  21. venkat says:

    add multiple configuration files by using , or space. Not , and space. Could you please clarify this one?

  22. Daksh says:

    Yes. Its was very helpful. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *