新增配置文件处理类:
/**
* 配置文件加载器
*
* @author 张成轩
*/
public class ConfigurerHandler extends PropertySourcesPlaceholderConfigurer implements
Converter<String, String> {
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
ConfigurablePropertyResolver propertyResolver) throws BeansException {
propertyResolver.getConversionService().addConverter(this);
super.processProperties(beanFactoryToProcess, propertyResolver);
}
/*
* 这里可以做配置文件的解密
*/
@Override
public String convert(String value) {
/* 这里做配置文件的解密处理,返回value就是配置项的值 */
return value;
}
}
修改Spring配置文件的引入方式:
正常配置文件引入
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:properties/app.properties" />
修改为
<!-- 引入配置文件 -->
<!-- 所有通过这种方式引入的配置文件,值都会通过加载器进行处理 -->
<bean class="com.loong.handler.ConfigurerHandler">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:properties/app.properties</value>
</list>
</property>
</bean>