What is the correct way to use the two contexts: dispatcher-servlet.xml
and applicationContext.xml
? What goes where?
I want to write a fair
The dispatcher-servlet.xml
file contains all of your configuration for Spring MVC
. So in it you will find beans such as ViewHandlerResolvers
, ConverterFactories
, Interceptors
and so forth. All of these beans are part of Spring MVC
which is a framework that structures how you handle web requests, providing useful features such as databinding, view resolution and request mapping.
The application-context.xml
can optionally be included when using Spring MVC
or any other framework for that matter. This gives you a container that may be used to configure other types of spring beans that provide support for things like data persistence. Basically, in this configuration file is where you pull in all of the other goodies Spring offers.
These configuration files are configured in the web.xml file as shown:
Dispatcher Config
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
WEB-INF/spring/servlet-context.xml
1
dispatcher
/
Application Config
contextConfigLocation
/WEB-INF/spring/application-context.xml
org.springframework.web.context.ContextLoaderListener
To configure controllers, annotate them with @Controller
then include the following in the dispatcher-context.xml
file: