<?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; best practices</title>
	<atom:link href="http://blog.codehangover.com/tag/best-practices/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>Choose many Spring contexts over a single context</title>
		<link>http://blog.codehangover.com/choose-many-spring-contexts-over-a-single-context/</link>
		<comments>http://blog.codehangover.com/choose-many-spring-contexts-over-a-single-context/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 13:29:40 +0000</pubDate>
		<dc:creator>MikeNereson</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.codehangover.com/?p=176</guid>
		<description><![CDATA[This is a short introduction to splitting up your bean definitions from one single Spring application context, to many application contexts. That is, from one XML file to many XML files. The general idea is to have one single primary application context, usually titled applicationContext.xml, and then many other application contexts with are named what they contain.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/choose-many-spring-contexts-over-a-single-context/";</script><p>This is a short introduction to splitting up your bean definitions from one single Spring application context, to many application contexts. That is, from one XML file to many XML files. The general idea is to have one single primary application context, usually titled applicationContext.xml, and then many other application contexts with are named what they contain.</p>
<h2>Example</h2>
<p>For example, a single web project might contain the following contexts.</p>
<ul>
<li> applicationContext.xml</li>
<li> applicationContext-dao.xml</li>
<li>applicationContext-dao-datasource.xml</li>
<li>applicationContext-ldap.xml</li>
<li> applicationContext-aspects.xml</li>
<li>applicationContext-web.xml</li>
</ul>
<h2>Breakdown</h2>
<p>In this example, your DAO definitions, transaction managers, and DAO or integration interceptors are defined in applicationContext-dao.xml. We have further broken the DAO application context to a dao-datasource which contains our data sources. LDAP configuration, beans, and DAOs are in their own applicationContext-ldap.xml.</p>
<h2>Benefits</h2>
<p>Why so many? There have been a few benefits so far.</p>
<h4>Responsibility</h4>
<p>The <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principal</a> is widely accepted and states that</p>
<blockquote><p>&#8230; every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility.states that every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility.</p></blockquote>
<p>The same holds true for XML contexts.  The general ides is to make our contexts more robust and to be able to isolate a context&#8217;s tasks and capabilities. And clearly, working with a smaller XML file is easier than working with one enormous file. It is easier to maintain and easier to read.</p>
<h4>Testing</h4>
<p>When I wrote my tests for LDAP, I had my LDAP configuration in applicationContext.xml, but then every time I ran an LDAP test, I would get the WebApplicationContext, which would import applicationContext-hibernate, which would connect to the database. Tests took forever and needless resources were used. Now I just get applicationContext-ldap.xml which only contains LDAP configurations, so no needless resources are loaded and tests remain quick.</p>
<h4>Inclusion and Exclusion</h4>
<p>Finally, its really easy to comment out a line like</p>
<pre class="brush: xml;">&lt;import resource=&quot;applicationContext-aspects.xml&quot;/&gt;</pre>
<p>and quickly have all my aspects, which for me only write logs, disabled. Or easy to add a line like</p>
<pre class="brush: xml;">&lt;import resource=&quot;applicationContext-dao-mock.xml&quot;/&gt;</pre>
<p>which overrides all of your DAO implementations with mock DAOs. This would be for testing.</p>
<p>Does anyone else use multiple application context files? See any other benefits? Or problems?<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/load-multiple-contexts-into-spring/" title="Load Multiple Contexts into Spring">Load Multiple Contexts into Spring</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%2Fchoose-many-spring-contexts-over-a-single-context%2F&amp;linkname=Choose%20many%20Spring%20contexts%20over%20a%20single%20context"><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/choose-many-spring-contexts-over-a-single-context/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

