我有不同环境的配置文件:prod.properties、dev. properties tites和test.properties。
属性文件通过application ation-context. xml文件加载,使用
${Spring。配置文件。活动}。属性
并传入-Dprint. profile.active标志。这适用于我的3个环境的文件。
对于我正在尝试使用的JUnit测试
@活动配置文件(“测试”)
在我的测试类中,但$(Spring. profile.active)没有解析。但是当我使用
System. getProperties().setProperty("Spring.profile.active","test");
它解决得很好。
我知道@ActiveProfiles仅适用于测试类,但为什么它不设置Spring. profile.active属性?它只影响要使用的bean吗?
这也没有阐明谁最适合处理配置文件和属性文件。Spring配置文件和测试
更新:到目前为止,我在我的应用程序上下文文件中使用了以下内容:
<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>
顾名思义@ActiveProfiles,这是配置文件数组。
/**
* The bean definition profiles to activate.
* <p>This attribute may <strong>not</strong> be used in conjunction with
* {@link #value}, but it may be used <em>instead</em> of {@link #value}.
*/
@AliasFor("value")
String[] profiles() default {};
尝试使用@ActiveProfiles({"test"})
。