我想使用 Spring 的属性占位符控制属性文件的加载并设置系统属性。这就像它应该的那样工作:
<context:property-placeholder
location="classpath:jdbc/jdbc.${TARGET_ENV}.properties" />
当我在不同的环境中运行我的应用程序时,我设置了系统属性 TARGET_ENV 并拾取了正确的属性文件。
现在我进行了集成测试,我想强制配置始终加载特定的属性文件(jdbc.INTEGRATION_TESTS.properties)。
我有
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/META-INF/spring/set-system-property.xml",
"classpath:/META-INF/spring/applicationContext.xml"})
public class AbstractIntegrationTest extends AbstractTransactionalJUnit4SpringContextTests {
和 set-system-property.xml (使用来自Set System Property With Spring Configuration File的建议):
<beans>
<bean id="systemPrereqs"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<util:properties>
<prop key="TARGET_ENV">INTEGRATION_TESTS</prop>
</util:properties>
</property>
</bean>
但是Spring没有收到该物业:
Caused by: java.io.FileNotFoundException: class path resource [jdbc/jdbc.${TARGET_ENV}.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
为什么这不起作用?我没有得到关于 bean 实例化、工厂 bean 等的东西吗?