我正在使用PropertyPlaceholderConfigurer在我的应用程序中加载属性文件,从中读取数据库详细信息并在数据源中动态替换它,如下所示。
<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>
上面的代码可以正常工作。 问题: 如果上述属性中有任何空格,则会导致应用程序失败。
Ex: mysql.jdbc.username= root
现在在上面的示例中,在用户名root之前有一个空格,因为我的应用程序无法连接到DB。 我接受这是一个人为的错误,但春天有办法自动处理它或在春天启用一些属性。
答案 0 :(得分:2)
PropertyPlaceholderConfigurer
没有直接支持修剪值,因为它假定您的属性来源是正确的。不过,介绍起来很简单。没有什么可以阻止你使用该类的自定义版本。
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();
}
}
答案 1 :(得分:0)
还有一个问题仍然处于状态&#34;未解决&#34;在Spring Jira。
https://jira.spring.io/browse/SPR-5839
希望我能帮助你。
BR