java - 如果条件基于 Activeprofile

标签 java spring-boot

出于测试目的,我正在尝试在内存中的 H2 中运行查询。由于 H2 限制,某些语法不起作用。我希望在 Spring Boot 中更改基于 @Activeprofile 的语法。我的代码看起来像这样:

if (@Activeprofile("Test")) {
    query = "something for test"
} else {
    query = "something for prod/stage" 
}

这可能吗?任何帮助表示赞赏。

最佳答案

您必须将Environment Bean 注入(inject)代码中。

像这样:

@Autowired
private Environment environment;

然后您可以使用 .getActiveProfiles() 方法。

if (Arrays.asList(environment.getActiveProfiles()).contains("...") {
    ...
}

有关此内容的更多信息,请参阅 here.

关于java - 如果条件基于 Activeprofile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55376620/

相关文章:

JAVA -> GWT -> JS 双值比较

java - 有没有一种聪明的方法来检查哈希表中的每个键是否都具有相同的值?

java - ubuntu上的运行时执行

java - 如何在akka actor中设置构造字段或配置加载?

java - 使用 gradle 为 spring-boot REST 服务运行集成测试

java - 如何在 Spring 关闭 prunsrv 服务属性?

java - 我可以在 Java BoxLayout 中混合水平和垂直框吗?

java - 在 Spring Boot 应用程序中启动 Eureka Server 时出错

spring-boot - Spring-Integration:异常时未发送 Tcp 服务器响应

java - 在 Spring v1.5.14.RELEASE 中模拟返回 List 的 RestTemplate