org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

本文介绍如何在Spring框架中整合配置文件,并通过PropertyPlaceholderConfigurer类实现属性值的动态替换。同时,展示如何扩展该类以对配置信息进行加密处理,确保信息安全。


可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。

从上图中,我们看到PropertyPlaceholderConfigurer实现了三个bean生命周期的接口:BeanFactoryAware & BeanNameAware & BeanFactoryPostProcessor。关于spring bean的生命周期,可以参考这里http://blog.youkuaiyun.com/gjb724332682/article/details/46767463

PropertyResourceConfigurer.postProcessBeanFactory()将properties文件中的属性进行merge,convert,最后调用PropertyPlaceholderConfigurer.processProperties()完成遍历bean定义替换属性占位符。


例子:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/conf/xx.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>


<!--当然也可以引入多个属性文件,如: -->


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath:conf/sqlmap/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="econsoleDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>


除此之外,我们还可以扩展自这个类,用来诸如加解密配置信息等操作。如下:

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.xxx.util.AESUtils;

public class DecryptPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {
	private String key = "xxxxxx";

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactory, Properties props)
			throws BeansException {
		try {
			String driverClassName = props.getProperty("driverClassName");
			if (driverClassName != null) {
				props.setProperty("driverClassName",
						AESUtils.aesDecrypt(driverClassName, key));
			}

			String url = props.getProperty("url");
			if (url != null) {
				props.setProperty("url", AESUtils.aesDecrypt(url, key));
			}

			String username = props.getProperty("username");
			if (username != null) {
				props.setProperty("username",
						AESUtils.aesDecrypt(username, key));
			}

			String password = props.getProperty("password");
			if (password != null) {
				props.setProperty("password",
						AESUtils.aesDecrypt(password, key));
			}
			super.processProperties(beanFactory, props);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BeanInitializationException(e.getMessage());
		}
	}
}
重写processProperties方法就可以。

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'apnsPayloadMap' defined in null: Could not resolve placeholder 'Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type' in value "${Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type' in value "${Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type}" at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:230) at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:211) at com.tplink.cloud.common.config.utils.CommonPropertyPlaceholderConfigurer.processProperties(CommonPropertyPlaceholderConfigurer.java:46) at com.tplink.cloud.msgcenter.config.PushConfig.processProperties(PushConfig.java:171) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:291) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:167) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at com.tplink.cloud.msgcenter.Bootstrap.initSpringContent(Bootstrap.java:123) ... 2 more Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type' in value "${Tapo.SelfCleanRobotSelfMopCleanCompleteEvent.msg.type}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:230) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:296) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:217) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitMap(BeanDefinitionVisitor.java:272) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:211) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:147) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85) at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:227)
最新发布
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值