如果一次只有一个配置文件处于活动状态,则可以执行以下操作。
<div th:if="${@environment.getActiveProfiles()[0] == 'production'}"> This is the production profile - do whatever you want in here</div>上面的代码基于Thymeleaf的Spring方言允许您使用该
@符号访问bean的事实。当然,该
Environment对象始终可以作为Spring
bean使用。
还要注意,
Environment该方法
getActiveProfiles()具有返回字符串数组的方法(这就是为什么
[0]在我的答案中使用的原因),我们可以使用标准Spring
EL进行调用。
如果一次有多个配置文件处于活动状态,则一种更可靠的解决方案是使用Thymeleaf的
#arrays实用程序对象,以检查
production活动配置文件中是否存在字符串。在这种情况下,代码为:
<div th:if="${#arrays.contains(@environment.getActiveProfiles(),'production')}"> This is the production profile</div>


