【发布时间】:2015-05-06 22:30:31
【问题描述】:
我有一个包含 jdbc.password={enc}laksksjdjdj 等值的属性文件
使用 JDK 1.7 和 Spring 4.1.5 我的配置类如下所示
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:env.properties")
})
@ComponentScan("com.acme")
@Configuration
public class SpringConfig
{
@Autowired
private ConfigurableEnvironment env;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{
return new EncryptedPropertySourcesPlaceholderConfigurer();
}
}
我想要实现的是将属性文件中的 any 值从加密值转换为实际值。有些值将被加密,有些则不会。这是我到目前为止所尝试的,当我在convertProperties() 上放置断点时,props 参数始终为空。我无法理解这一点,因为我可以看到此时 this.environment 已加载文件中的所有属性。
public class EncryptedPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer
{
@Override
protected void convertProperties(Properties props)
{
Enumeration<?> propertyNames = props.propertyNames();
while (propertyNames.hasMoreElements()) {
String propertyName = (String) propertyNames.nextElement();
String propertyValue = props.getProperty(propertyName);
// String convertedValue = <translate the value>;
props.setProperty(propertyName, convertedValue);
}
}
@Override
protected Properties mergeProperties() throws IOException {
final Properties mergedProperties = super.mergeProperties();
convertProperties(mergedProperties);
return mergedProperties;
}
}
有没有人能够使用PropertySourcesPlaceholderConfigurer 实现这一目标?我有类似的逻辑在使用 PropertyPlaceholderConfigurer 的旧应用程序中工作,但想使用较新的 Spring 配置。
我注意到 Jasypt 有一个类似的 EncryptablePropertySourcesPlaceholderConfigurer,但它的行为方式相同,所以我很困惑。
【问题讨论】:
标签: java spring properties jasypt