• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Give me the differences between applicationContext.xml and mvc-dispatcher-servlet.xml in SpringMVC

 
rajesh babu Y
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using this
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>mvc-dispatcher-servlet.xml</param-value>
</context-param>

i'm getting exception like ...

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/mvc-dispatcher-servlet.xml]


Also tried with

<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

1) what is difference between applicationContext.xml and mvc-dispatcher.servlet.xml ?

2) Why Dispatcher servlet always looking for the init-param contextConfigLocation as "applicationContext.xml" only, if i gave something else it's throwing the IOException and saying not able to get "applicationContext.xml"

3) If i'm not giving the <init-param> and gave <context-param> contextConfigLocation as "<servlet-name>-servlet.xml", it's working fine. Then is there any difference in bean scope declaration if we configured as <context-param> instead <init-param>

4) <context-param> contextConfigLocation is always taking "<servlet-name>-servlet.xml", are we have chance to change that name ?

5) If we're not able change the <context-param> contextConfigLocation value, how we can provide multiple context for an application in spring MVC ?
 
Saravana Pandiyan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajesh,

XML names are just for our identification purpose, means by seeing the name will come to know that whats the use of that xml file or what does it contains.

Say for example

applicationContext.xml - Will tell you that contains information related to your applicationContext.
mvc-servlet.xml - MVC related configurations.
database.xml - database related configuration.
etc...

For your second question, i mean how to change the default <servlet-name>-servlet.xml in Spring MVC.

Do like this:



Instead of giving your mvc configuration file as a <context-param>, give as a <init-param>.

Regards,
dspandiyan
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different scopes to Spring Web contexts.

Have a look at this example web.xml from the Spring pet clinic example:
https://github.com/SpringSource/spring-petclinic/blob/master/src/main/webapp/WEB-INF/web.xml

The root context is available to all servlets. This is usually where you keep you service and repository beans.



Then you can have another context scoped to each dispatcher servlet (yes you could have more than one mapped to different URL's although this is pretty uncommon).



This context is where you put your controller beans. These beans can also see all the beans defined in the root context listed above but not vice versa.
 
rajesh babu Y
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill /Saravana Pandiyan,

i gave the web.xml entry for dispatcher servlet

<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>


I'm getting following error

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/Spring MVC Dispatcher Servlet-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/Spring MVC Dispatcher Servlet-servlet.xml]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)



1) Do we need to provide the <context-param> contextConfigLocation always as <servlet-name>-servlet.xml only ?

2) If yes, how should i integrate another module configuration file in application ?
 
rajesh babu Y
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/welcome"
class="com.rajesh.common.controller.HelloWorldController" />
<bean name="/*.ma"
class="com.rajesh.common.controller.CustomAction" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>



mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/welcome"
class="com.rajesh.common.controller.HelloWorldController" />
<bean name="/*.ma"
class="com.rajesh.common.controller.CustomAction" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic