如果条件基于Activeprofile

时间:2019-03-27 11:56:09

标签: java spring-boot

我试图在H2内存中运行查询以进行测试。由于H2的限制,某些语法不起作用。我希望根据@Activeprofile中的Spring Boot更改语法。我的代码如下所示:

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

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

1 个答案:

答案 0 :(得分:3)

您必须在代码中注入一个Environment Bean。

赞:

@Autowired
private Environment environment;

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

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

有关更多信息,请参见here.

相关问题