External Properties File for Spring Web Site Configuration

In planning a public facing demo site of my NixMash Spring app I want to reduce maintenance by disabling registration. I couldn’t use the approach I blogged about in limiting actions based on role since all users viewing the Registration Page would be anonymous users. My solution was an external properties file, which Spring makes very easy to implement and Thymeleaf very easy to put into play.

Here’s the simple ApplicationSettings class where we currently have a single isDemoSite boolean property.

You can see the location of the properties file in the @PropertySource annotation along with the prefix, so for the public site the isDemoSite property will appear in external.properties as:

external.isDemoSite=false

Making it Happen with Thymeleaf

We’re disabling the Registration Form button if isDemoSite = true. In Thymeleaf, disabling the button is a simple matter as well.

<button type="submit" class="btn btn-primary" th:text="#{register.form.submit}"
th:disabled="${appSettings.getIsDemoSite()}">[Submit]</button>

Oh, I almost forgot to mention how Thymeleaf knows about the appSettings class. That is in a @ControllerAdvice Spring class which serves up the class as a model to all pages.

@ModelAttribute("appSettings")
public ApplicationSettings getApplicationSettings() {
return applicationSettings;
}

The source code in this post can be found in the v0.1.6 branch of NixMash Spring on GitHub.