Is there any way to automatically trim values ​​from Spring property files?

For example, I have a property in the application.properties file:

server.numbers = sss01, sss04 

and, accordingly, I get this array of values ​​in the code as follows:

 @Value("${server.numbers:}") private String[] numbers; 

And at the same time, after receiving, I have to trim the spaces by performing the trim() operation for each element of the array. Is it possible to do this automatically, using Spring tools, for example, through annotation?

  • One solution is to write your own custom PropertyPlaceholderConfigurer as indicated here - Denis Vabishchevich
  • @Nikolay, yes, a good question) just had a different value in the properties file (I edited the message to match the reality), and when writing the message I deleted the characters before the numbers) - Ksenia

1 answer 1

I tried to reproduce your example and everything works out of the box for me (spring 1.5.9):

 @Component public class NumbersComponent { @Value("${server.numbers:}") private String[] numbers; @PostConstruct public void postConstruct() { Arrays.stream(numbers).map(n -> "|" + n + "|").forEach(System.out::println); } } 

Result:

| sss01 |
| sss04 |