0

Spring Boot – Validate .properties Configurations when Application Starts Up

This post demonstrates how to validate application configurations during start-up to avoid invalid values in run-time.

Requirements

Stuff used in this post.

  • Java 8 (JDK)
  • IntelliJ IDEA Ultimate 2016.3
  • Spring Boot 1.5.6.RELEASE

Our Configurations

Let’s say we have these important configurations for some modules and we need to validate against some rules for those configurations.

Our Modules and their Rules

Module 1 and 2 have these rules:

  • timeoutInSeconds must not be null and between 30 and 120 inclusively.
  • allowedGroups must not be null, and have at least 1 but at most 3 groups.

Module1ConfigBean

Module2ConfigBean

Testing

Our main class is simple.

It outputs:

If we correct the .properties file and modify the main class a bit, the output will be:

Download the codes

https://github.com/Turreta/Spring-Boot-Validate-.properties-Configurations-when-Application-Starts-Up