できるよ:
<context:property-placeholder location="classpath:${spring.profiles.active}.properties" />
正常に動作しますが、同時に複数のプロファイルを使用する場合はおそらく適応されません。
2つのプロパティプレースホルダーを宣言するときに、最初のプレースホルダーにすべてのアプリケーションキーが含まれていない場合は、2番目のプレースホルダーを使用できるようにunresolvable=trueを無視して属性を配置する必要があります。それがあなたのやりたいことかどうかはわかりませんが、xx1プロファイルとxx2プロファイルの両方を同時にアクティブにしたい場合はそうかもしれません。
このように2つのpropertyplaceholdersを宣言すると、それらが独立し、xx2.propertiesの宣言では、xx1.propertiesの値を再利用できないことに注意してください。
より高度なものが必要な場合は、アプリケーションの起動時にPropertySourcesを登録できます。
web.xml
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.xxx.core.spring.properties.PropertySourcesApplicationContextInitializer</param-value>
</context-param>
作成するファイル:
public class PropertySourcesApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertySourcesApplicationContextInitializer.class);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
LOGGER.info("Adding some additional property sources");
String profile = System.getProperty("spring.profiles.active");
// ... Add property sources according to selected spring profile
// (note there already are some property sources registered, system properties etc)
applicationContext.getEnvironment().getPropertySources().addLast(myPropertySource);
}
}
完了したら、コンテキストに追加する必要があります。
<context:property-placeholder/>
スプリングプロパティを処理するのに最適な方法です。ローカルプロパティをどこでも宣言しないため、何が起こっているかをプログラムで制御でき、プロパティソースのxx1値をxx2.propertiesで使用できます。
仕事で私たちはそれを使用していて、それはうまく機能します。3つの追加のプロパティソースを登録します。-インフラストラクチャ:Puppetによって提供されます-プロファイル:プロファイルに従ってロードされた別のプロパティ。-共通:すべてのプロファイルが同じ値を共有する場合など、デフォルトとして値が含まれます。