Johnny
Johnny

Reputation: 678

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

I'm using PropertiesFactoryBean to load properties from a typical Properties file. Is there anyway to get Spring to automatically trim trailing white space from the prop value?

Upvotes: 8

Views: 13416

Answers (6)

Florent Dupont
Florent Dupont

Reputation: 1936

One easy way to do it would be to "hack" the spEl Expression to force the use of the String.trim() function.

Let's say you have a property test.myvalue equal to azerty (with trailing spaces) in the application.properties file, then you could inject this property in your class like this :

@Value("#{'${test.myvalue}'.trim()}")
String myvalue;

The resulting myvalue will be equal to azerty (no trailing spaces) once injected in your class.

Obviously this trimming won't be set globally to all injected values in your app, and you'll have to do it to all injected value, but I think this approach gives more flexibility.

Upvotes: 0

Thomas Kainrad
Thomas Kainrad

Reputation: 2830

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.

Upvotes: 8

wonhee
wonhee

Reputation: 1661

With latest Spring version(4.3+), you can simply call setTrimValues() with true when you create PropertySourcesPlaceholderConfigurer bean in your configuration. That will remove any extra leading or trailing spaces from the value you got from the properties file.

Upvotes: 3

fante76
fante76

Reputation: 55

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.

Upvotes: 3

Carlos D.
Carlos D.

Reputation: 435

You can define your own property configurer:

package your.company.package;

public class TrimPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

@Override
protected String resolvePlaceholder( String placeholder, Properties props ) {
        String value = super.resolvePlaceholder( placeholder, props );

        return (value != null ? value.trim() : null );
    }
}

Then you must define it in your bean_config.xml

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:main.properties" />
</bean>

<bean id="trimPropertyPlaceholderConfigurer" class="your.company.package.TrimPropertyPlaceholderConfigurer">
    <property name="properties" ref="applicationProperties" />
</bean>

Another way if you're using @Value annotations to set the properties into the fields:

@Value( value = "#{applicationProperties['my.app.property'].trim()}" )

NullPointerException is thrown if the property doesn't exists in the file

Upvotes: 1

Kris Babic
Kris Babic

Reputation: 6304

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.

Upvotes: 4

Related Questions