[英]How to set annotation attribute value based on the active profile in Spring Boot?
[英]How can I set active profile via annotation in spring?
如何在弹簧中通过注释设置活动轮廓?
例如:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles( profiles = {ApplicationProfiles.TEST} )
public class CacheManagerTest {
...
}
对于JUnit测试,这种方法很完美,但是如何初始化生产应用程序上下文呢? (我没有任何主要方法/сlasses)
您可以使用spring.profiles.active
属性在运行时传入活动的配置文件:
-Dspring.profiles.active="profile1,profile2"
有关配置文件的介绍,请参阅SpringSource博客文章 。
根据Spring博客的说法,如果您正在使用独立的应用程序或Web应用程序,则可以通过这些方式传递活动配置文件
在Web应用程序中激活
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
使用手动创建的上下文激活
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.load("classpath:/com/bank/config/xml/*-config.xml");
ctx.refresh();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.