3

Spring を使用すると、ある種の環境 (dev|test|prod) 固有のプロパティが必要になります。

私は構成ファイル (myapp.properties) を 1 つだけ持っていますが、何らかの理由で複数の構成ファイルを持つことはできません (Spring でさえ複数を処理できます)。

したがって、次のような接頭辞を持つプロパティを追加する可能性が必要です

dev.db.user=foo
prod.db.user=foo

VM-argument などで使用するプレフィックス (環境) をアプリケーションに伝えます-Denv-target

4

3 に答える 3

2

複数の構成ファイルを持つことを避けるための制約が何であるかはわかりませんが、 -Denvtarget=someValue のようなものを使用して Java で行うことができます:

//Obtain the value set in the VM argument 
String envTarget= System.getProperty("env-target");

Properties properties;
try {
   properties = PropertiesLoaderUtils.loadAllProperties("myapp.properties");
} catch (IOException exception) {
  //log here that the property file does not exist.    
}

//use here the prefix set in the VM argument.
String dbUser = properties.getProperty(envTarget+".db.user");
于 2013-08-14T20:48:20.937 に答える