Get spring application environment in thymeleaf

后端 未结 2 1467
暖寄归人
暖寄归人 2021-01-31 09:05

My Spring Boot application runs with 3 configurations:

  • application.properties --> for development environment
  • application-test.properties --> for test env
2条回答
  •  日久生厌
    2021-01-31 09:38

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

    This is the production profile - do whatever you want in here

    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:

    This is the production profile

提交回复
热议问题