<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Hangover &#187; applicationContext</title>
	<atom:link href="http://blog.codehangover.com/tag/applicationcontext/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codehangover.com</link>
	<description>Go ahead, have another</description>
	<lastBuildDate>Tue, 22 Mar 2011 15:49:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Load Multiple Contexts into Spring</title>
		<link>http://blog.codehangover.com/load-multiple-contexts-into-spring/</link>
		<comments>http://blog.codehangover.com/load-multiple-contexts-into-spring/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 13:32:47 +0000</pubDate>
		<dc:creator>MikeNereson</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[applicationContext]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.codehangover.com/?p=168</guid>
		<description><![CDATA[How do you load more than one Spring application context? There are a couple of ways to do this.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/load-multiple-contexts-into-spring/";</script><p>I have  already argued that <a href="http://blog.codehangover.com/choose-many-spring-contexts-over-a-single-context/" target="_blank">many application contexts are better than a single application context</a>. But how do you load more than one context?</p>
<p>There are a couple of ways to do this.</p>
<h2>web.xml contextConfigLocation</h2>
<p>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.</p>
<pre class="brush: xml;">
&lt;context-param&gt;
    &lt;param-name&gt;
        contextConfigLocation
    &lt;/param-name&gt;
    &lt;param-value&gt;
        applicationContext1.xml
        applicationContext2.xml
    &lt;/param-value&gt;
&lt;/context-param&gt;

&lt;listener&gt;
    &lt;listener-class&gt;
        org.springframework.web.context.ContextLoaderListener
    &lt;/listener-class&gt;
&lt;/listener&gt;
</pre>
<p>The above uses carriage returns. Alternatively, yo could just put in a space.</p>
<pre class="brush: xml;">
&lt;context-param&gt;
    &lt;param-name&gt;
        contextConfigLocation
    &lt;/param-name&gt;
    &lt;param-value&gt;
        applicationContext1.xml applicationContext2.xml
    &lt;/param-value&gt;
&lt;/context-param&gt;

&lt;listener&gt;
    &lt;listener-class&gt;
        org.springframework.web.context.ContextLoaderListener
    &lt;/listener-class&gt;
&lt;/listener&gt;
</pre>
<h2>applicationContext.xm import resourcel</h2>
<p>Your other option is to just add your primary applicationContext.xml to the web.xml and then use import statements in that primary context.</p>
<p>In applicationContext.xml you might have…</p>
<pre class="brush: xml;">
&lt;!-- hibernate configuration and mappings --&gt;
&lt;import resource=&quot;applicationContext-hibernate.xml&quot;/&gt;

&lt;!-- ldap --&gt;
&lt;import resource=&quot;applicationContext-ldap.xml&quot;/&gt;

&lt;!-- aspects --&gt;
&lt;import resource=&quot;applicationContext-aspects.xml&quot;/&gt;
</pre>
<h2>Which strategy should you use?</h2>
<p>I always prefer to load up via web.xml 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.</p>
<p>If you are loading contexts into a non-web application, I would use the import resource.</p>
<p>Any benefits to going with the application context import method over the web.xml contextConfigLocation?<br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://blog.codehangover.com/spring-patterns-best-practices-and-design-strategies-book-review/" title="Spring Patterns: Best Practices and Design Strategies Book Review">Spring Patterns: Best Practices and Design Strategies Book Review</a></li>
<li><a href="http://blog.codehangover.com/getting-friendly-with-spring-junit-and-easymock/" title="Getting friendly with Spring, JUnit and EasyMock.">Getting friendly with Spring, JUnit and EasyMock.</a></li>
<li><a href="http://blog.codehangover.com/choose-many-spring-contexts-over-a-single-context/" title="Choose many Spring contexts over a single context">Choose many Spring contexts over a single context</a></li>
</ul>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.codehangover.com%2Fload-multiple-contexts-into-spring%2F&amp;linkname=Load%20Multiple%20Contexts%20into%20Spring"><img src="http://blog.codehangover.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.codehangover.com/load-multiple-contexts-into-spring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

