我们使用Spring和jasypt EncryptablePropertyPlaceholderConfigurer
来读取application.properties文件。
如果某些属性值最后包含空格,则有时会出现问题,在使用@Value(${})
标记读取该值时,我们最后也会得到尾随空格,这会产生问题。
现在类EncryptablePropertyPlaceholderConfigurer
是final,所以不能扩展,我搜索了很多,想知道在修剪字符串值周围的空白之后是否有任何方法可以获得属性。
有人能建议如何处理这种情况吗?
您可以使用构造函数中传递的自定义StringEncryptor创建EncryptablePropertyPlaceholderConfigurer。在这个CustomStringEncryptor.decrypt()中执行trim()。(在这种情况下,你不知道你正在解密的属性是什么)
您可以通过授权绕过决赛:
class CustomStringEncryptor implements StringEncryptor{
private StringEncryptor delegate;
public CustomStringEncryptor(StandardPBEStringEncryptor delegate){
this.delegate = delegate;
}
String decrypt(String encryptedMessage){
String message = this.delegate.decrypt(encryptedMessage);
if(null != message) message = message.trim();
return message;
}
}
所以我在"bellabax"的帮助下找到了问题的答案我否决了Properties persister并实现了我自己的方法
propertyConfigurator.setPropertiesPersister(new MyDefaultPropertiesPersister());
@Override
public void load(Properties props, InputStream is) throws IOException {
props.load(is);
for (Entry<Object, Object> property : props.entrySet()) {
property.setValue(property.getValue().toString().trim());
}
}
现在我的属性去掉了尾随空格我希望这对某些人有所帮助。