Search code examples
javaspring-bootconfigurationthymeleaf

Configuration of thymeleaf text template in spring boot application.properties failed


I'm trying to configure a simple thymeleaf text template in my spring boot configuration and got to the point where the thymeleaf variable placeholder and the spring boot configuration property placeholder interfer (afaik both use the SpEL) when the template contains a colon (e.g. "[(${#dates.format(date, 'dd-MM-yyyy HH:mm')})]" as spring boot tries to resolve the variable "#dates.format(date, 'dd-MM-yyyy HH" but uses default value "mm')" instead.

I tried to change the spring boot prefix of the PropertySourcesPlaceholderConfigurer, but then some of my included libraries no longer work as they use the ${} variables

Is there a way to extend thymeleaf to treat %{} like ${} ?

I want to avoid replacing the template i read from the config, as i configure those templates on various properties and classes

ad1: For configuration i use a custom mechanism that converts a xml file into yaml which will then be used for configuration, so basically you can say its a application.yml configuration file. I cannot use custom template files in this scenario as the customer must be able to configure the template in a custom xml editor. The templates are all just a few words (like the subject of an email e.g.)


Solution

  • You can protect your expression by wrapping certain placeholders, thus hiding them from Spring Boot PropertyResolver mechanism:

    application.properties:

    test-template=[(#{'$'}{#dates.format(date, 'dd-MM-yyyy HH:mm')})]
    
    

    see full working code here: https://github.com/62mkv/spring-properties-thymeleaf.

    This solution is "loaned" from here: https://github.com/spring-projects/spring-framework/issues/9628