作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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 之前有一个空格,因此我的应用程序无法连接到数据库。我承认这是一个人为错误,但是 spring 有没有办法自动处理它或通过在 spring 中启用某些属性来处理它。
最佳答案
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();
}
}
关于java - 有没有办法修剪使用 PropertyPlaceholder 读取的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30891387/
我是一名优秀的程序员,十分优秀!