我有一个主 app-context.xml,它定义了一个具有两个位置的属性占位符:默认属性文件和一个可选的覆盖文件:
<context:property-placeholder
location="classpath:config.properties,${configOverride}"
ignore-resource-not-found="true" />
可选的覆盖位置允许指定另一个属性文件(例如“-DconfigOverride=file:/home/app/config.properties”),其中只包含应该被覆盖的属性。
对于我的单元测试,我使用了一个导入 app-context.xml 的测试上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class UserServiceTest {
...
}
在加载应用程序上下文之前,如何在应用程序中设置系统属性或环境变量?如果可能的话,我想达到与在所有测试类中设置“-DconfigOverride=classpath:testConfig.properties”相同的效果,而不必指定命令行参数。