. properties文件读取及占位符${…}替换源码解析

  

<强>前言

  

我们在开发中常遇到一种场景,豆里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在. properties文件中,然后在Bean实例化的时候通过春天将这些. properties文件中配置的参数使用占位符“${}”替换的方式读入并设置到Bean的相应参数中。

  

这种做法最典型的就是JDBC的配置,本文就来研究一下. properties文件读取及占位符“${}”替换的源码,首先从代码入手,定义一个数据源,模拟一下JDBC四个参数:

        公共类数据源{/* *   *驱动类   */私人字符串driveClass;/* *   * jdbc地址   */私人字符串url;/* *   *用户名   */私人字符串的用户名;/* *   *密码   */私人密码字符串;      公共字符串getDriveClass () {   返回driveClass;   }      公共空间setDriveClass(字符串driveClass) {   这一点。driveClass=driveClass;   }      公共字符串getUrl () {   返回的url;   }      公共空间setUrl(字符串url) {   这一点。url=url;   }      公共字符串getUserName () {   返回用户名;   }      公共空间setUserName(字符串的用户名){   这一点。用户名=用户名;   }      公共字符串getPassword () {   返回密码;   }      公共空间向setPassword(字符串密码){   这一点。密码=密码;   }      @Override   公共字符串toString () {   返回“数据源(driveClass=" + driveClass +”, url=url“+ +”,用户名=" +用户名+”,密码=" +密码+“]”;   }   }      之前      

定义一个db.properties文件:

        driveClass=0   url=1   用户名=2   密码=3      

定义一个properties.xml文件:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;豆类xmlns=" http://www.springframework.org/schema/beans "   xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance "   xmlns: aop=" http://www.springframework.org/schema/aop "   xmlns: tx=" http://www.springframework.org/schema/tx "   xsi: schemaLocation=" http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd”比;      & lt; bean类=皁rg.springframework.beans.factory.config.PropertyPlaceholderConfigurer”比;   & lt;属性名="位置" value=" https://www.yisu.com/zixun/properties/db.properties "祝辞& lt;/property>   & lt;/bean>      & lt; bean id=笆菰础崩?皁rg.xrq.spring.action.properties.DataSource”比;   & lt;属性名=" driveClass " value=" https://www.yisu.com/zixun/$ {driveClass} "/比;   & lt;属性名=" url " value=" https://www.yisu.com/zixun/$ {url} "/比;   & lt;属性名="用户名" value=" https://www.yisu.com/zixun/$ {userName} "/比;   & lt;属性名="密码" value=" https://www.yisu.com/zixun/${密码}"/比;   & lt;/bean>   & lt;/beans>   之前      

写一段测试代码:

        公开课TestProperties {      @Test   公共空间testProperties () {   ApplicationContext ac=new ClassPathXmlApplicationContext(“春/properties.xml”);      数据源的数据源=(数据源)ac.getBean(“数据源”);   System.out.println(数据源);   }   }   之前      

运行结果就不贴了,很明显,下面就来分析一下春天是如何将属性文件中的属性读入并替换" ${}"占位符的。

  

<强> PropertyPlaceholderConfigurer类解析

  

在properties.xml文件中我们看到了一个类PropertyPlaceholderConfigurer,顾名思义它就是一个属性占位符配置器,看一下这个类的继承关系图:

  

 . properties文件读取及占位符${…}替换源码解析

  

看到从这张图上,我们能分析出来的最重要的一点就是PropertyPlaceholderConfigurer是BeanFactoryPostProcessor接口的实现类,想见。

  

<强> . properties文件读取源码解析

  

下面来看一下postProcessBeanFactory方法实现:

        公共空间postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory)抛出BeansException {   尝试{   属性mergedProps=mergeProperties ();//将合并后的属性,如果必要的。   convertProperties (mergedProps);//让子类处理属性。   processProperties (beanFactory mergedProps);   }   抓住(IOException ex) {   把新BeanInitializationException(“无法加载属性”,前);   }   }      

. properties文件读取及占位符${…}替换源码解析