1

I am using PropertyPlaceholderConfigurer to load a property file in my application from that I reading the database details and replacing it dynamically in data source as shown below.

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

<bean id="mysqlSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="${mysql.jdbc.url}" />
    <property name="user" value="${mysql.jdbc.username}" />
    <property name="password" value="${mysql.jdbc.password}" /> </bean>

The above code works fine as expected. Problem: If there is any space in the above said properties value will leads to application failure.

Ex: mysql.jdbc.username= root

Now in the above example there is a space before user name root because of which my application fails to connect to DB. I accept it is a human error but is there a way in spring to handle it automatically or by enabling some properties in spring.

Krishna
  • 198
  • 2
  • 2
  • 12

2 Answers2

2

There is no direct support in the PropertyPlaceholderConfigurer for trimming values because it assumes your property source is correct. Still, it's simple to introduce. There is nothing stopping you from using a custom version of the class.

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    @Override
    protected String resolvePlaceholder(String placeholder, Properties props) {
        String value = super.resolvePlaceholder(placeholder, props);
        return value == null ? null : value.trim();
    }

}
m4ktub
  • 3,061
  • 1
  • 13
  • 17
0

actually I am not aware of such an Mechanism. But this seems to be a duplicate of Automatically Trim Trailing White Space for properties in Props file loaded into Spring

Also there is an Issue which is still in status "Unresolved" in Spring Jira.

https://jira.spring.io/browse/SPR-5839

Hope I could help you.

BR

Community
  • 1
  • 1
questionaire
  • 2,475
  • 2
  • 14
  • 28