<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4210742358358282564</id><updated>2024-08-28T18:02:55.212-07:00</updated><category term="java"/><category term="springframework"/><title type='text'>tastybyte</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tastybyte.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4210742358358282564/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tastybyte.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Lars</name><uri>http://www.blogger.com/profile/01250360266472369504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4210742358358282564.post-776751735453612155</id><published>2007-05-29T19:11:00.000-07:00</published><updated>2007-05-29T19:36:57.583-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="springframework"/><title type='text'>Environment-specific config with Spring</title><content type='html'>&lt;h1&gt;The Problem&lt;/h1&gt;&lt;br /&gt;After a jaunt working with Rails I&#39;m back in Java-land.  Now that I&#39;ve tasted the sweet simplicity I&#39;m jonesin&#39; to feed it back into my Java projects.&lt;br /&gt;&lt;br /&gt;So here&#39;s a little hack that let me apply my Rails idiom of environment/[development|test|production] to the Java world.&lt;br /&gt;&lt;h1&gt;The Implementation&lt;/h1&gt;&lt;h3&gt;The config files&lt;/h3&gt;&lt;i&gt;development.properties&lt;/i&gt;:&lt;br /&gt;&lt;pre&gt;jdbc.driverClassName=oracle.jdbc.OracleDriver&lt;br /&gt;jdbc.url=jdbc:oracle:thin:@...&lt;br /&gt;&lt;br /&gt;hibernate.dialect=org.hibernate.dialect.OracleDialect&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;i&gt;test.properties&lt;/i&gt;:&lt;br /&gt;&lt;pre&gt;jdbc.driverClassName=org.hsqldb.jdbcDriver&lt;br /&gt;jdbc.url=jdbc:hsqldb:mem:unittest&lt;br /&gt;jdbc.username=sa&lt;br /&gt;jdbc.password=&lt;br /&gt;&lt;br /&gt;hibernate.dialect=org.hibernate.dialect.HSQLDialect&lt;br /&gt;&lt;/pre&gt;&lt;i&gt;Same deal with production.properties&lt;/i&gt;&lt;br /&gt;&lt;h3&gt;Creating the ApplicationContext&lt;/h3&gt;It&#39;s not a web-app so I&#39;m doing it programatically.&lt;br /&gt;&lt;pre&gt;beanFactory = new ClassPathXmlApplicationContext(new String[] {&quot;beans.xml&quot;}, false);&lt;br /&gt;&lt;br /&gt;PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();&lt;br /&gt;configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);&lt;br /&gt;&lt;br /&gt;String environment = System.getProperty(&quot;environment&quot;, &quot;development&quot;);&lt;br /&gt;File customProperties = new File(&quot;/etc/conf/custom.properties&quot;);&lt;br /&gt;configurer.setLocations(new Resource[] {new ClassPathResource(environment + &quot;.properties&quot;), new FileSystemResource(customProperties)});&lt;br /&gt;&lt;br /&gt;beanFactory.addBeanFactoryPostProcessor(configurer);&lt;br /&gt;beanFactory.refresh();&lt;br /&gt;&lt;/pre&gt;&lt;h3&gt;Unit tests&lt;/h3&gt;I also created a mini spring file &lt;i&gt;beans-test.xml&lt;/i&gt;:&lt;br /&gt;&lt;pre&gt;&amp;lt;beans&amp;gt;&lt;br /&gt;&amp;lt;bean class=\&amp;quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer\&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;property name=\&amp;quot;locations\&amp;quot;&amp;gt;&lt;br /&gt; &amp;lt;value&amp;gt;classpath:test.properties&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;lt;/property&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;br /&gt;&lt;/pre&gt;All it does is load in my test.properties.&lt;br /&gt;&lt;h5&gt;My test superclass&lt;/h5&gt;&lt;pre&gt;import org.springframework.test.AbstractDependencyInjectionSpringContextTests;&lt;br /&gt;&lt;br /&gt;public abstract class TestSupport extends AbstractDependencyInjectionSpringContextTests {&lt;br /&gt;public AbstractMdsIntegrationTestSupport() {&lt;br /&gt;setPopulateProtectedVariables(true);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected String[] getConfigLocations() {&lt;br /&gt;return new String[] {&quot;classpath:/beans-test.xml&quot;, &quot;classpath:/beans.xml&quot;};&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;Results&lt;/h1&gt;Now I can check out a clean codebase and run the unit tests and also fire up my application with the main() function, both from my IDE with no flag, properties file editing, etc.  Then I can switch to production mode with just a -Denvironment=production.&lt;br /&gt;&lt;br /&gt;You may have caught the &lt;i&gt;customProperties&lt;/i&gt; file, that&#39;s how I keep from having to wrangle my production passwords into the classpath or system properties.</content><link rel='replies' type='application/atom+xml' href='http://tastybyte.blogspot.com/feeds/776751735453612155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4210742358358282564/776751735453612155' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4210742358358282564/posts/default/776751735453612155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4210742358358282564/posts/default/776751735453612155'/><link rel='alternate' type='text/html' href='http://tastybyte.blogspot.com/2007/05/environment-specific-config-with-spring.html' title='Environment-specific config with Spring'/><author><name>Lars</name><uri>http://www.blogger.com/profile/01250360266472369504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>24</thr:total></entry></feed>