gpt4 book ai didi

java - 有没有办法修剪使用 PropertyPlaceholder 读取的所有值

转载 作者:行者123 更新时间:2023-11-30 03:23:01 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com