如果你不希望你的外部化屬性文件:
我使用的是代表我的財產我的部署環境的前綴。例如:
#Test url
test.url=http://test.url.com
#Production URL
prod.url=http://prod.url.com
我定義了一個名爲每個環境中的「ENTORN」(-D參數調用JVM在應用服務器的啓動腳本)的系統性能。此屬性的值在我的測試環境中爲「測試」,在我的生產環境中爲「prod」。
然後我定義我的「propertyConfigurer」豆:
<bean id="propertyConfigurer" class="es.indra.ccma.config.EnvironmentPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:ccma.properties</value>
</list>
</property>
</bean>
的EnvironmentPropertyPlaceholderConfigurer代碼:
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);
}
}
}
如果運行在「測試」環境代碼,並要檢索的價值「url」屬性,propertyConfigurer在屬性文件中查找「test.url」,如果沒有找到「test.url」屬性,它會查找「url」屬性。
這不是我的想法I followed this tutorial來實現這一點。
不錯,實際上並不需要將環境配置置於固定的根位置,/ site/properties就像$ {env.applicationRoot}/config一樣簡單。我喜歡 – ptomli 2009-08-28 11:27:53