开发者

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 tr开发者_如何学Pythonailing white space from the prop value?


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.


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 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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜