Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't want to externalize your properties file:</p> <p>I'm using a prefix that represents my deployment environment in my properties. Example:</p> <pre><code>#Test url test.url=http://test.url.com #Production URL prod.url=http://prod.url.com </code></pre> <p>I defined a system property named "entorn" in each environment (-D argument to jvm call in your application server start script). The value of this property is "test" in my test environment and "prod" in my production environment.</p> <p>Then I defined my "propertyConfigurer" bean:</p> <pre><code>&lt;bean id="propertyConfigurer" class="es.indra.ccma.config.EnvironmentPropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:ccma.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>The EnvironmentPropertyPlaceholderConfigurer code:</p> <pre><code>package es.indra.ccma.config; import java.util.Properties; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class EnvironmentPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private String environment; final private static String ENV_SYSTEM_PROPERTY = "entorn"; public EnvironmentPropertyPlaceholderConfigurer() { environment = System.getProperty(ENV_SYSTEM_PROPERTY); if (environment == null) { //default environment environment = "test"; } } protected String resolvePlaceholder(String placeholder, Properties props) { final String envPlaceholder = environment + "." + placeholder; if (props.containsKey(envPlaceholder)) { return props.getProperty(envPlaceholder); } else { return props.getProperty(placeholder); } } } </code></pre> <p>If you are running your code in "test" environment and you want to retrieve the value of "url" property, the propertyConfigurer looks for "test.url" in your properties files and if no "test.url" property found it will look for "url" property.</p> <p>This is not my idea <a href="http://clintshank.javadevelopersjournal.com/spring_configuration_per_environment.htm" rel="nofollow noreferrer">I followed this tutorial</a> to acomplish this.</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