Automatically Trim Trailing White Space for properties in Props file loaded into Spring

You can customize the Properties loading functionality by passing in a custom PropertiesPersister into your PropertiesFactoryBean configuration. The PropertiesPersister instance is used by the PropertiesFactoryBean to parse the Properties file data. The default implementation follows the native parsing of java.util.Properties. You can customize the parsing logic by providing your own implementation of the PropertiesPersister interface.


As this can often be a source of confusion when using Spring Boot, I want to add that you do not need XML configuration to provide your own PropertyPlaceholderConfigurer.

Simply put this in your main class:

  @Bean
  public static PropertySourcesPlaceholderConfigurer createPropertyConfigurer()
  {
    PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertyConfigurer.setTrimValues(true);
    return propertyConfigurer;
  }

This is sufficient for trimming the values from application.properties.


As Chad said, Spring solved this problem with version 4.3RC1. But you need to manually set on trim function with parameter "trimValues" like so (default if "false"):

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="trimValues" value="true"/>
   <property name="locations">
       <list>
        ...
       </list>
   </property>

I do not found any documentation about this but I deduce it from Spring API.