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:
DispatcherServlet
AND the context-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?
Yes, in large projects, having multiple Spring configurations increase maintainability and modularity. You can also upload one XML file that will contain all configs.
Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app.
It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a config directory off the root) but then you will need to have your build management tool (e.g. Ant or Maven ) copy it into the classpath for the runtime.
I find the following setup the easiest.
Use the default config file loading mechanism of DispatcherServlet:
The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).
In your case, simply create a file intrafest-servlet.xml
in the WEB-INF
dir and don't need to specify anything specific information in web.xml
.
In intrafest-servlet.xml
file you can use import to compose your XML configuration.
<beans> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> <import resource="foo-services.xml"/> <import resource="foo-persistence.xml"/> </beans>
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.
Mike Nereson has this to say on his blog at:
http://blog.codehangover.com/load-multiple-contexts-into-spring/
There are a couple of ways to do this.
1. web.xml contextConfigLocation
Your first option is to load them all into your Web application context via the ContextConfigLocation element. You’re already going to have your primary applicationContext here, assuming you’re writing a web application. All you need to do is put some white space between the declaration of the next context.
<context-param> <param-name> contextConfigLocation </param-name> <param-value> applicationContext1.xml applicationContext2.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
The above uses carriage returns. Alternatively, yo could just put in a space.
<context-param> <param-name> contextConfigLocation </param-name> <param-value> applicationContext1.xml applicationContext2.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
2. applicationContext.xml import resource
Your other option is to just add your primary applicationContext.xml to the web.xml and then use import statements in that primary context.
In
applicationContext.xml
you might have…<!-- hibernate configuration and mappings --> <import resource="applicationContext-hibernate.xml"/> <!-- ldap --> <import resource="applicationContext-ldap.xml"/> <!-- aspects --> <import resource="applicationContext-aspects.xml"/>
Which strategy should you use?
1. I always prefer to load up via web.xml.
Because , this allows me to keep all contexts isolated from each other. With tests, we can load just the contexts that we need to run those tests. This makes development more modular too as components stay
loosely coupled
, so that in the future I can extract a package or vertical layer and move it to its own module.2. If you are loading contexts into a
non-web application
, I would use theimport
resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With