spring 改变占位符属性值

需求:

    像数据库密码,一般不能在属性文件中明文表示。那么需要将密码加密,在配置文件中配置加密后的密文。运行时候,加载属性文件后再将密文还原为明文,提供给jdbc驱动作为连接数据库参数。

实现:

java:

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {

		Properties p = decodeProperties(props);//此处解密属性
		super.processProperties(beanFactoryToProcess, p);
	}
}

 xml配置:

  	<!--  原来占位符属性
	<bean id="webframe.datasource.config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		-->
	<bean id="webframe.datasource.config" class="com.skyon.webframe.config.DecryptPropertyPlaceholderConfigurer">	
		<property name="order" value="1" />
    	<property name="ignoreUnresolvablePlaceholders" value="true" />
    	<property name="properties" ref="mm" />
        <!--  mm此处属性使用密文-->
	</bean>

猜你喜欢

转载自xinglijun1973.iteye.com/blog/2377875