String之PropertyPlaceholderConfigurery源码解析

1. PropertyPlaceholderConfigurery使用


注解方式:

<context:property-placeholder location="classpath*:disconf.properties" 
 ignore-unresolvable="true"
	ignore-resource-not-found="true" order = "2" />


bean方式:

<bean id="disconfStaticConfigPropertyConfigurer"
		  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!--找不到文件时不报错-->
		<property name="ignoreResourceNotFound" value="true"/>
		<!--是否应该忽略不可解析的占位符-->
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
		<!--order越小,越先被执行-->
		<property name="order" value="1"/>
		<!--本地配置文件-->
		<property name="location" value="classpath*:disconf.properties" />
		<property name="propertiesArray">
			<list>
				<!--Properties对象引用-->
				<ref bean="disconfPropertiesReader"/>
			</list>
		</property>
	</bean>

2. PropertyPlaceholderConfigurery原理

PropertyPlaceholderConfigurery继承关系



PropertyResourceConfigurer实现了BeanFactoryPostProcessor接口,在Spring读取了所有bean的配置元数据,但是还没有实例化的时候,完成读取配置文件,替换占位符。


1.mergeProperties
    1.1 从本地配置读取
    1.2 合并传递进来的properties

2.convertProperties
    2.1替换properties中value中的占位符

3.processProperties
    3.1 替换spring BeanDefinition中ParentName,BeanClassName,FactoryBeanName,FactoryMethodName,Scope,
PropertyValues,ConstructorArgumentValues中的占位符
    3.2 替换spring Aliases别名中的占位符
    3.3 addEmbeddedValueResolver添加到嵌入式值解析器列表中

3. 动态读取PropertyPlaceholderConfigurery

public class PropertyTest implements EmbeddedValueResolverAware {

    private String tempFilePath;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        //默认解析${}中的参数,
        // 可以在定义PropertyPlaceholderConfigurery时设置placeholderPrefix,placeholderSuffix自定义分隔符
        tempFilePath = resolver.resolveStringValue("${temp.file.path}");
    }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值