How to load freemarker templates from external file system folder with Spring Boot

For a project we are currently working, we use freemarker templates for sending emails to the customers. Sometimes we need to change this templates on the fly to change some text parts or fix some typos. As default spring boot looks in /templates folder in the classpath for freemarker (actually any type of) templates and thus any change on templates needs actually a rebuild and redeploy of the application.

In fact spring boot is flexible enough to configure freemarker’s template loader to search templates in different folders even outside of the application’s jar file like a file system folder or http resource.

According to Common application properties document (the official spring boot documentation) we can configure the template path by placing the spring.freemarker.template-loader-path property into the file.

# Comma-separated list of template paths.
spring.freemarker.template-loader-path=file:./templates/, classpath:/templates/

With above example we say to template loader, “Dear Template Loader, please look first the ‘templates’ folder right beside of our application’s jar file and then please (if you can’t find the desired template) look into ‘templates’ folder in the classpath”.

To ensure the hot detection of changes in the templates files loaded from file system you may add following property in your file. (Actually you don’t need this, because hot loading is the default configuration)

# Prefer file system access for template loading. File system access enables hot detection of template changes.

Stay happy until our next blog entry. 🙂

Leave a reply