Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Don't add logic to your code to test which environment you're running in - that is a recipe for disaster (or at least burning a lot of midnight oil down the road).</p> <p>You use Spring, so take advantage of it. Use dependency injection to provide environment-specific parameters to your code. E.g. if you need to call a web service with different endpoints in test and production, do something like this:</p> <pre><code>public class ServiceFacade { private String endpoint; public void setEndpoint(String endpoint) { this.endpoint = endpoint; } public void doStuffWithWebService() { // use the value of endpoint to construct client } } </code></pre> <p>Next, use Spring's <a href="http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-extension-factory-postprocessors">PropertyPlaceholderConfigurer</a> (or alternatively PropertyOverrideConfigurer) to populate this property from either a .properties file, or from a JVM system property like so:</p> <pre><code>&lt;bean id="serviceFacade" class="ServiceFacade"&gt; &lt;property name="endpoint" value="${env.endpoint}"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;value&gt;classpath:environment.properties&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>Now create two (or three, or four) files like so - one for each of the different environments.</p> <p>In environment-dev.properties:</p> <pre><code>env.endpoint=http://dev-server:8080/ </code></pre> <p>In environment-test.properties:</p> <pre><code>env.endpoint=http://test-server:8080/ </code></pre> <p>Now take the appropriate properties file for each environment, rename it to just environment.properties, and copy it to your app server's lib directory or somewhere else where it will appear on your app's classpath. E.g. for Tomcat:</p> <pre><code>cp environment-dev.properties $CATALINA_HOME/lib/environment.properties </code></pre> <p>Now deploy your app - Spring will substitute the value "http://dev-server:8080/" when it sets up your endpoint property at runtime.</p> <p>See the Spring docs for more details on how to load the property values.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload