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 :)
its very good to know this new feature of spring …
is it available in all spring version ?
thankx i was searching for this
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
Not very helpful 2 me…
the data is not there whatever i m looking 4.
Can you configure multiple init params in DispatcherServlet ?
flexspring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
spring.profiles.active
production
1
Very good explanation ever seen this information thank you very much
Good and basic thing one should know to start with Spring.
Yeah.. Its very useful….and.basic thing one should know to start with Spring.
Excellent tutorial. Worked fine.
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
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?
Tnx very much, this tutorial solve a mbig doubt looking at the Spring MVC Showcase tutorial
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
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
Thx a lot, Excellent tutorial !!!
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.
…or if file name pattern is same then in above case we can write /WEB-INF/bean*.xml for all three files
Thank you so much, good job keep it up.
Nice work and easily explained
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.
add multiple configuration files by using , or space. Not , and space. Could you please clarify this one?
Yes. Its was very helpful. Thanks