我有我的不同环境的配置文件:prod.properties,dev.propertites和test.properties。用于junit测试的setProperty和ActiveProfiles注释的区别
属性文件是通过使用
$应用context.xml文件加载{} spring.profiles.active的.properties
并传递-Dspring.profiles.active标志。这适用于我的3环境。
针对JUnit测试我想在我的测试类使用
@ActiveProfiles( “测试”)
但是$(spring.profiles.active)没有解决。但当我使用
System.getProperties()。setProperty(“spring.profiles.active”, “test”);
它解决得很好。
我知道@ActiveProfiles仅适用于测试类,但为什么不设置spring.profiles.active属性?它只影响使用哪些bean?
这也不能说明谁最适合处理配置文件和属性文件。 Spring profiles and Testing
更新: 我迄今采取使用我的应用程序上下文文件如下:
<beans profile="dev,prod">
<context:property-placeholder
location="classpath:META-INF/properties/generic.properties,
classpath:META-INF/properties/${spring.profiles.active}/${spring.profiles.active}.properties"/>
<context:component-scan base-package="com.my.package.to.scan"/>
</beans>
<beans profile="test">
<context:property-placeholder
location="classpath:META-INF/properties/generic.properties,
classpath:META-INF/properties/test/test.properties"/>
<context:component-scan base-package="com.my.package.to.scan"/>
</beans>
我希望它是那么简单...仍然没有拿起它...造成:java.io.FileNotFoundException:类路径资源[META-INF/properties/$ {spring.profiles.active}/$ {spring.profiles.active} .properties]无法打开,因为它不存在 –
为了让它起作用,我必须在应用程序内容文件中为属性占位符定义bean配置文件。对于我的envinronments,我在属性文件路径中保存了$ {spring.profiles.active}属性,但为了junit的目的,我使用它进行了硬编码。 –
@MichoRizo FileNotFoundException与您最初提到的有所不同。因为它可以分享进一步的信息,如项目结构您的文件位置。如果您提出新问题,我认为这不需要进行硬编码,因此更好。 – Rohit