<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Spring @ clintshank.javadevelopersjournal.com</title><link>http://clintshank.javadevelopersjournal.com/</link><description>(Spring) My thoughts on software development.</description><copyright>Copyright 2008 clintshank.javadevelopersjournal.com</copyright><generator></generator><lastBuildDate>Fri, 05 Sep 2008 17:00:00 GMT</lastBuildDate><image><title>Spring @ clintshank.javadevelopersjournal.com</title><url>http://res.sys-con.com/portlet/163/featured-blog-graphic-145.gif</url><link>http://clintshank.javadevelopersjournal.com/</link></image><ttl>360</ttl><docs>http://backend.userland.com/rss</docs><item><title>Spring Configuration Per Environment</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/spring_configuration_per_environment.htm</guid><link>http://clintshank.javadevelopersjournal.com/spring_configuration_per_environment.htm</link><pubDate>Wed, 30 Jan 2008 16:10:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=spring%5Fconfiguration%5Fper%5Fenvironment</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>It&#39;s common that configuration changes between environments (e.g., development, test, production).&nbsp; For example, you&#39;d definitely want a different DataSource URL depending on what environment you&#39;re running in.&nbsp; Do you really want to hit the production database while developing?&nbsp; Spring doesn&#39;t seem to support this per environment configuration out of the box (at least I&#39;ve never been able to find it).&nbsp; There&#39;s been a request for this&nbsp;feature&nbsp;for some time now (<a href="http://jira.springframework.org/browse/SPR-1876">SPR-1876</a>), but it&#39;s still open.</p><p>The reality is that properties files are the simplest and easiest way to configure an application, particularly for non-developers.&nbsp; What I&#39;d like to see is a <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html">PropertyPlaceholderConfigurer</a> that can configure an application per environment.&nbsp; This is what SPR-1876 is all about.&nbsp; However, it suggests there be a file per environment.&nbsp; As an alternative, I suggest a single properties file.&nbsp; A single file containing all the properties would be the simplest to maintain.&nbsp; Think of it.&nbsp; I&#39;ve got a new property.&nbsp; Do I want to add it to three files or one?&nbsp; If I want to see all the possible values of a property, do I want to open and hunt through three files or one?</p><p><a href="http://grails.org/doc/1.0.x/guide/3.%20Configuration.html#3.2%20Environments">Grails</a>&nbsp;supports the idea of grouping all the properties together, but it uses&nbsp;multiple configuration files (Config.groovy and DataSource.groovy) depending on the property.</p><p>Okay, so here&#39;s what I propose the properties file look like:</p><pre># this is the default value for all environments<br />my.prop=defaultValue<br /># but in the production environment, it is different<br />[production].my.prop=productionValue<br /> <br /># here&#39;s another property with no default<br /># (adapted example from the Grails user guide)<br />[development].dataSource.url=jdbc:hsqldb:mem:devDB<br />[test].dataSource.url=jdbc:hsqldb:mem:testDb<br />[production].dataSource.url=jdbc:hsqldb:file:prodDb;shutdown=true<br /> </pre><p>The context definition simply uses the property name without the environment and the custom per environment PropertyPlaceholderConfigurer provides the appropriate value:</p><pre>&lt;bean id=&quot;dataSource&quot;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class=&quot;org.springframework.jdbc.datasource...&quot;&gt;<br />&nbsp; &lt;property name=&quot;url&quot; value=&quot;${dataSource.url}&quot; /&gt;<br />&lt;/bean&gt;</pre><p>So what are the downsides to this approach?&nbsp; I suppose you can argue that a single properties file could be cluttered with details you don&#39;t care about.&nbsp; For example, a production administrator may not care to see all the development and test values.</p><p>The other downside I can think of is forgetting to override a property.&nbsp; That is, nothing reminds you that you have to override the DataSource URL for production.&nbsp; It&#39;s content with using a default value.&nbsp; The separate file approach would handle this because the property would be undefined.</p><p>So if I were reading this I&#39;d say, &quot;Spring is open source dude.&nbsp; And those Spring guys are busy.&nbsp; Go implement it and attach it to SPR-1876.&quot;&nbsp; And if I get some time... </p><p>&nbsp;</p>]]></description><category>spring</category></item></channel></rss>