Spring Boot — How to hot swap Thymeleaf templates

This is an often-asked question on the Internet — and one which, previously, had no good answer. Now, thanks to @Jianrong Chen, we have a one-liner configuration to do it!

application.properties

# Thymeleaf templates from filesystem, not cached
#
spring.thymeleaf.cache=false
spring.thymeleaf.templates_root=file:///${user.dir}/src/main/resources/templates/

# static content from filesystem first, too
#
spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static

The magic here is to set ‘spring.thymeleaf.templates_root’ to load from the filesystem, rather than the classpath. This means the project no longer has to be built.. Thymeleaf picks up changed templates immediately.

The amazing thing is that apart from one tiny comment on Stack Overflow, this seems completely undocumented — the world is unaware of this.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *