PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer

Spirng在生命周期里关于Bean的处理大概可以分为下面几步:
- 加载 Bean 定义(从xml或者从@Import等)
 - 处理 BeanFactoryPostProcessor
 - 实例化 Bean
 - 处理 Bean 的 property 注入
 - 处理 BeanPostProcessor
 
而当我们在声明了
<context:property-placeholder location="classpath:config.properties"/>
标签之后,即声明了一个配置型 bean 交给 Spring 容器进行管理,即 PropertyPlaceholderConfigurer 类。我们先看一下这个类的继承结构。

这里 PrepertyPlaceholderConfigurer 实现了 BeanFactoryPostProcesser 接口,并实现了 postProcessBeanFactory() 方法,即当 Spring 容器的 BeanFactory 被构造成功之后会调用这个方法。这时,我们先看父类的 PropertyResourceConfigurer 方法 postProcessBeanFactory。因为这个类继承了 Spring 的 BeanFactoryPostProcesser 接口,所以这个方法一定是操作 BeanFactory 的。
org.springframework.beans.factory.config.PropertyResourceConfigurer
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    try {
    //1. 获取当前容器配置的所有Properties文件,可能由多个文件merge而来
        Properties mergedProps = mergeProperties();
        //2. 如果需要的话,将Properties文件的内容进行转化,因为默认的Preperties都是String的key-value形式。
        //   Spring提供的默认方式是不转化,保持String,String的key-value
        convertProperties(mergedProps);
        //3. 由子类继承,对容器与Properties进行操作,即value注入。
        processProperties(beanFactory, mergedProps);
    }
    catch (IOException ex) {
        throw new BeanInitializationException("Could not load properties", ex);
    }
}
这里最重要的第一步就是获得 Properties 文件即 mergeProperties 方法,这是解析资源文件最基本的方法,所以这个方法一定存在于当前功能的最基类中,即 PropertiesLoaderSupport。由于xml中是这样配置的:
<context:property-placeholder location="classpath:config.properties"/>
这里声明了一个 PropertyPlaceholderConfigurer 对象,显然是调用了 setLocation 方法,而这个方法同样存在于该功能模块的最基本父类 PropertiesLoaderSupport 中。
org.springframework.core.io.support.PropertiesLoaderSupport
public abstract class PropertiesLoaderSupport {
    private Resource[] locations;
    public void setLocation(Resource location) {
        this.locations = new Resource[] {location};
    }
    //注意:后声明的文件覆盖先声明的文件,以最后一个文件为准
    public void setLocations(Resource... locations) {
        this.locations = locations;
    }
}
mergeProperties 方法中进行了配置化管理,将从 this.locations 中加载的 Properties 与 localProperties 合并,localOverride 控制覆盖顺序:
protected Properties mergeProperties() throws IOException {
    Properties result = new Properties();
    if (this.localOverride) {
        // Load properties from file upfront, to let local properties override.
        loadProperties(result);
    }
    if (this.localProperties != null) {
        for (Properties localProp : this.localProperties) {
            CollectionUtils.mergePropertiesIntoMap(localProp, result);
        }
    }
    if (!this.localOverride) {
        // Load properties from file afterwards, to let those properties override.
        loadProperties(result);
    }
    return result;
}
// 加载配制文件到 Properties 中
protected void loadProperties(Properties props) throws IOException {
    if (this.locations != null) {
    //1.遍历声明的Resource文件地址
        for (Resource location : this.locations) {
            if (logger.isInfoEnabled()) {
                logger.info("Loading properties file from " + location);
            }
            try {
        //2.获得Resource文件流,并加载内容到Properties对象中
                PropertiesLoaderUtils.fillProperties(
                        props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
            }
            catch (IOException ex) {
                if (this.ignoreResourceNotFound) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
                    }
                }
                else {
                    throw ex;
                }
            }
        }
    }
}
回想 PropertyResourceConfigurer 主流程中的三个方法,第一步已经执行完毕,加载了配置的 properties 文件,第二步是 spring 自己的默认实现,将非空的 key 对应的 value 放入 Properties 中,第三步则该由子类各自实现了,将 BeanFactory 与 Properties 进行统一操作。这时候我们看我们直接声明的派生类 PropertyPlaceholderConfigurer。
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException {
    //1.声明一个支持value为String类型的Resolver
    StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
    //2.将key-value注入到BeanFactory的某些bean中
    doProcessProperties(beanFactoryToProcess, valueResolver);
}
接下来就是真正的 value 注入环节了
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
            StringValueResolver valueResolver) {
    //1. 将key-value内容声明为BeanDefinitionVisitor对象,用来根据BeanDefinition修改即将生成的对应的Bean内容
    BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (String curName : beanNames) {
        //2. 只有同一个容器内的才可以进行value注入,同时应该避免掉操作本身,避免进入循环递归
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            try {
                visitor.visitBeanDefinition(bd);
            }
            catch (Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName,
                    ex.getMessage(), ex);
            }
        }
    }
    //3.处理一些拥有别名的类
    beanFactoryToProcess.resolveAliases(valueResolver);
    //4.New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.(这一步有些不懂,以后再修正)
     beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
}
在上述代码中,第2步已经修改了原始的 BeanDefinition,我们一路跟进去看,原来核心的替换功能在 PropertyPlaceholderHelper 中:
protected String parseStringValue(
        String strVal, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
    //1. 对每一个key进行处理
    StringBuilder result = new StringBuilder(strVal);
    //2. 首先考虑有占位符的情况,默认是${}
    int startIndex = strVal.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
        // 考虑 key 占位符嵌套 ${${${}}},先查找外层 ${} 成对出现的最后一个 '}'
        int endIndex = findPlaceholderEndIndex(result, startIndex);
        if (endIndex != -1) {
            String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            String originalPlaceholder = placeholder;
            if (!visitedPlaceholders.add(originalPlaceholder)) {
                throw new IllegalArgumentException(
                        "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
            }
            //3. 如果 key 有占位符,即 key=${abc},则递归调用本方法查找 key 的真实值
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
            //4. 真正的从 key-value 集合中获得 key 对应的真实值
            String propVal = placeholderResolver.resolvePlaceholder(placeholder);
            //5. 如果没有找到,则试图按照 ${key:default} 的形式解析
            if (propVal == null && this.valueSeparator != null) {
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    //5.1 获得:之前的内容,即真正的key
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    //5.2 获得:之后的内容,即默认值
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    //5.3 再次尝试从key-value集合中获得内容,因为如果真的是key-value的形式,按照全名是肯定找不到的
                    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                    //5.4 如果找到了就按照配置的走,如果没有找到则附上默认值
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            //6. 如果最终解析到 propVal,则还要判断 propVal 是否有占位符,即 propVal=${} 的情况
            if (propVal != null) {
                //6.1 如果找到了这个value,则再次递归调用自己,避免value也是占位符的情况
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                //6.2 将获得的结果替换掉
                result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            }
            else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            }
            else {
                throw new IllegalArgumentException("Could not resolve placeholder '" +
                        placeholder + "'" + " in string value \"" + strVal + "\"");
            }
            visitedPlaceholders.remove(originalPlaceholder);
        }
        else {
            startIndex = -1;
        }
    }
    return result.toString();
}
参考:
https://www.cnblogs.com/kingszelda/p/7261156.html
http://blog.csdn.net/qq_28580959/article/details/60129329
PropertyPlaceholderConfigurer的更多相关文章
- PropertiesFactoryBean PropertyPlaceholderConfigurer 区别
		
正如 stackoverflow上说的,PropertiesFactoryBean 是PropertiesLoaderSupport 直接的实现类, 专门用来管理properties文件的工厂bean ...
 - Spring PropertyPlaceholderConfigurer数据库配置
		
pom.xml中添加依赖 <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId ...
 - spring的多个PropertyPlaceholderConfigurer实例装配的问题
		
1. 默认情况下,使用PropertyPlaceholderConfigurer多实例装配出现异常 在项目中尝试 在不同的spring的配置文件中分别引入相应的properties文件,这样会在spr ...
 - Spring里PropertyPlaceholderConfigurer类的使用
		
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
 - 读取配置文件 PropertyPlaceholderConfigurer 的配置与使用
		
public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map< ...
 - PropertyPlaceholderConfigurer的用法:
		
用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...
 - Spring中PropertyPlaceholderConfigurer的使用
		
Spring中PropertyPlaceholderConfigurer的使用 在使用Spring配置获取properties文件时,在网上查到相关的资料,分享哈!! (1)获取一个配置文件 ...
 - spring PropertyPlaceholderConfigurer 找不到配置文件原因
		
1: spring 版本问题 参见: http://www.cnblogs.com/alex-blog/archive/2012/12/25/2832357.html 2: bean id 同名 ...
 - PropertyPlaceholderConfigurer加载属性配置文件:
		
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
 - spring中propertyplaceholderconfigurer简介
		
Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...
 
随机推荐
- 12.13java过关测试
			
库存物资管理系统: 1.首先建两个数据表,一个pass用于商品的增删改查,一个passd记录商品的出库与入库信息: 2.建两个实体类,用来传递商品与单据的信息 3.在添加与删除时,填写入库或者出库单据 ...
 - Linux yum安装MySQL5.7
			
一.安装配置MySQL的yum源 # 安装MySQL的yum源,下面是RHEL6系列的下载地址 rpm -Uvh http://dev.mysql.com/get/mysql-community-re ...
 - leetcode 167 two sum II
			
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
 - pta l2-18(多项式A除以B)
			
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984 题意:给定两个多项式,求出其做除法 ...
 - CentOS 下搭建Hudson
			
1.下载Hudson安装包 wget http://ftp.jaist.ac.jp/pub/eclipse/hudson/war/hudson-3.3.3.war 2.执行 java -jar hud ...
 - poj 1789 prime
			
链接:Truck History - POJ 1789 - Virtual Judge https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...
 - 高级测试岗位面试题---MARK
			
直接手写一个python类 直接手写一个构造函数 紧接着上面的代码,直接手写,补充完整代码,要求对列表中的人进行排序,并筛选出分数大于80的人的名单,组成一个新的列表显示出来. class Perso ...
 - [leetcode]133. Clone Graph 克隆图
			
题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node neighbor 1 2, 3 2 1 3 1 ...
 - java NIO  Buffer 详解(1)
			
1.java.io 最为核心的概念是流(stream),面向流的编程,要么输入流要么输出流,二者不可兼具: 2.java.nio 中拥有3个核心概念: Selector Channel, Buffe ...
 - 定时器修改button标题闪烁
			
在做一个项目时,用到UIButton来设置接收短信验证码的倒计时,但是用NSTimer来设置标题会出现连续闪烁的问题. 经过测试发现了一下内容,如果只是单独的设置button的titleLabel的内 ...