38

My Spring Boot application runs with 3 configurations:

  • application.properties --> for development environment
  • application-test.properties --> for test environment
  • application-production.properties --> for production environment

How is it possible to get in thymeleaf environment the application is running?

I need to include the Google Analytics code only in production environment.

Zergleb
  • 2,212
  • 15
  • 24
occurred
  • 490
  • 1
  • 5
  • 13

2 Answers2

60

You can do the following if you only have one profile active at a time.

<div th:if="${@environment.getActiveProfiles()[0] == 'production'}">
  This is the production profile - do whatever you want in here
</div>

The code above is based on the fact that the Thymeleaf's Spring dialect lets you access beans using the @ symbol. And of course the Environment object is always available as a Spring bean.

Also note that Environment has the method getActiveProfiles() which returns an array of Strings (that is why [0] is used in my answer) which we can call using standard Spring EL.

If more than one profiles are active at a time, a more robust solution would be to use Thymeleaf's #arrays utility object in order to check for the presence of the string production in the active profiles. The code in that case would be:

<div th:if="${#arrays.contains(@environment.getActiveProfiles(),'production')}">
     This is the production profile
</div>
geoand
  • 60,071
  • 24
  • 172
  • 190
  • @occurred Glad to help! It would help future readers if you accept the answer, because that way that could easily tell if it's correct without having to read the comments – geoand May 18 '14 at 17:59
  • 20
    Meanwhile there is also `acceptsProfiles(String... profiles)` in `Environment`, which makes the check a little smoother: `@environment.acceptsProfiles('production')` – cfrick Oct 28 '14 at 14:14
  • 1
    Note that [`Environment.acceptsProfiles(String ...)`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.html#acceptsProfiles(java.lang.String...)) has been deprecated as of v5.1. I've opened [Issue #30206](https://github.com/spring-projects/spring-framework/issues/30206) asking if they want to reconsider deprecation, pointing them to this discussion here. – Garret Wilson Mar 27 '23 at 20:54
  • Instead of `acceptsProfiles(String…)`, which has been deprecated, you should now be using `matchesProfiles(String...)`. See [Spring Framework Issue #30206](https://github.com/spring-projects/spring-framework/issues/30206). – Garret Wilson Jul 30 '23 at 15:10
5

Simply add this class which allows to set global variables for views:

@ControllerAdvice
public class BuildPropertiesController {

    @Autowired
    private Environment env;

    @ModelAttribute("isProd")
    public boolean isProd() {
        return Arrays.asList(env.getActiveProfiles()).contains("production");
    }
}

And then use ${isProd} variable in your thymeleaf file:

<div th:if="${isProd}">
     This is the production profile
</div>

Or you can set active profile name as a global variable:

@ControllerAdvice
public class BuildPropertiesController {

    @Autowired
    private Environment env;

    @ModelAttribute("profile")
    public String activeProfile() {
        return env.getActiveProfiles()[0];
    }
}

And then use ${profile} variable in your thymeleaf file (if you have one active profile):

<div>
     This is the <span th:text="${profile}"></span> profile
</div>
Igorock
  • 2,691
  • 6
  • 28
  • 39