What is the correct way to split Spring's configuration to multiple xml files?
At the moment I have
/WEB-INF/foo-servlet.xml
/WEB-INF/foo-service.xml
/WEB-INF/foo-persistence.xml
My web.xml
has the following:
<servlet>
<description>Spring MVC Dispatcher Servlet</description>
<servlet-name>intrafest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/foo-*.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/foo-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
The actual questions:
- Is this approach correct/best?
- Do I really need to specify the config locations both in the
DispatcherServlet
AND thecontext-param
sections?
What do I need to keep in mind to be able to reference beans defined in foo-servlet.xml
from foo-service.xml
? Does this have something to do with specifying contextConfigLocation
in web.xml
?
Update 1:
I'm using Spring framework 3.0. It's my understanding that I don't need to do resource importing like this:
<import resource="foo-services.xml"/>
Is this a correct assumption?
Best Answer
I find the following setup the easiest.
Use the default config file loading mechanism of DispatcherServlet:
In your case, simply create a file
intrafest-servlet.xml
in theWEB-INF
dir and don't need to specify anything specific information inweb.xml
.In
intrafest-servlet.xml
file you can use import to compose your XML configuration.Note that the Spring team actually prefers to load multiple config files when creating the (Web)ApplicationContext. If you still want to do it this way, I think you don't need to specify both context parameters (
context-param
) and servlet initialization parameters (init-param
). One of the two will do. You can also use commas to specify multiple config locations.