システム プロパティを使用して、環境固有のプロパティ ファイルの場所を定義しています。ただし、統合テスト用にその値を別のものにオーバーライドしたいと思います。
これが私のプロダクションスプリングのセットアップです。カスタム PropertyPlaceholderConfigurer を使用して暗号化されたプロパティ ファイルの値を解決していますが、ここでは重要ではありません。
<-- Spring configuration in file service-spring-beans.xml -->
<bean class="com.mycompany.MyPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/${MY_ENVIRONMENT}/${MY_ENVIRONMENT}.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
実行時に、MY_ENVIRONMENT の値を Java システム プロパティとして定義します。これはすべて期待どおりに機能します。ただし、統合テストの場合、MY_ENVIRONMENT を「inttest」として定義したいので、統合テスト固有のプロパティ ファイル properties/inttest/inttest.properties が読み込まれます。
統合テストによってロードされたスプリング コンテキストを使用して、ID MY_ENVIRONMENT を持つ文字列 Bean をセットアップしようとしました。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.mycompany.myclasses"/>
<bean class="java.lang.String" id="MY_ENVIRONMENT">
<constructor-arg value="inttest"/>
</bean>
<!-- this imports the production spring context -->
<import resource="classpath:service-spring-beans.xml"/>
</beans>
ただし、MY_ENVIRONMENT の値は解決されず、統合テストの実行時にこのエラーが発生します。
原因: org.springframework.beans.factory.BeanInitializationException: プロパティを読み込めませんでした。ネストされた例外は java.io.FileNotFoundException: クラス パス リソース [properties/${MY_ENVIRONMENT}/${MY_ENVIRONMENT}.properties] が存在しないため開けません
システム プロパティを JVM に渡さずに、inttest 時に MY_ENVIRONMENT をオーバーライドするにはどうすればよいですか?