0

Spring Boot – Trim String values from .properties files

This post demonstrates how to trim string values from .properties files using SpringApplicationBuilder and a subclass of or anonymous class of StandardEnvironment supplied to the builder with overridden getProperty(String) and getProperty(String, String) methods.

Requirements

Stuff used in this post.

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

Use-case

So we have this item in the .properties file with a string with length 9. The first 4 characters are digits, the rest are white spaces which are usually trimmed off in some applications.

What if we have a lot of these properties that need trimming?

Solution

The solution is basically to trim the String values when they are read (or accessed) anywhere in the codes. This can be achieved by overriding some methods of StandardEnvironment and using SpringApplicationBuilder.

This outputs:

Download the codes

https://github.com/Turreta/Spring-Boot-Trim-String-values-from-.properties-files

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert