浅谈Spring的PropertyPlaceholderConfigurer

大型项目中,我们往往会对我们的系统的配置信息进行统一管理,一般做法是将配置信息配置与一个cfg.properties的文件中,然后在我们系统初始化的时候,系统自动读取cfg.properties配置文件中的key value(键值对),然后对我们系统进行定制的初始化。

  那么一般情况下,我们使用的java.util.Properties,也就是java自带的。往往有一个问题是,每一次加载的时候,我们都需要手工的去读取这个配置文件,一来编码麻烦,二来代码不优雅,往往我们也会自己创建一个类来专门读取,并储存这些配置信息。

 但是,在于我看来,这些原本Spring就有的东西,你还自己重复造轮子,费功夫。而且未必没有那么方便维护和管理升级。
用起来的通用性比较差,各个企业有各个企业的开发规范,这样子一来,学习成本也提高。(这个是废话)。

Spring中提供着一个PropertyPlaceholderConfigurer

  这个类是BeanFactoryPostProcessor的子类。(不懂自己度娘,可了解可不了解,下面我会讲大概)

其主要的原理在是。Spring容器初始化的时候,会读取xml或者annotation对Bean进行初始化。
初始化的时候,这个PropertyPlaceholderConfigurer会拦截Bean的初始化,
初始化的时候会对配置的${pname}进行替换,根据我们Properties中配置的进行替换。从而实现表达式的替换操作 。

了解完原理之后,我们来看其实现的几种方式。

在我们的classpath下新建一个cfg.properties

#cfg.properties配置文件的内容
username=jay    
password=123

下面是Spring配置文件代码

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 对于读取一个配置文件采取的方案 -->
        <property name="location" value="classpath:cfg.properties"></property>
        <!-- 对于读取两个以上配置文件采取的处理方案 -->
        <!--
        <property name="locations"> 
            <list>
                <value>classpath:cfg.properties</value>
                <value>classpath:cfg2.properties</value>
            </list>
        </property>
        -->
    </bean>
</beans>
//测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationCtx.xml")
public class SpringCtxTst {


    @Value("${username}")
    private String uname;

    @Value("${password}")
    private String pwd;

    @Test
    public void test(){
        System.out.println("username:"+uname);
        System.out.println("password:"+pwd);
    }
}

控制台输出

username:jay 
password:123

当然,Spring 为此也为我们提供了另外一个的解决方案,我们直接在Spring 配置文件中书写如下代码即可实现

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!--采用这种方式简化配置文件-->
    <context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/>

</beans>

重要注意

  我们知道,不论是使用PropertyPlaceholderConfigurer还是通过context:property-placeholder这种方式进行实现,都需要记住,Spring框架不仅仅会读取我们的配置文件中的键值对,而且还会读取Jvm 初始化的一下系统的信息。有时候,我们需要将配置Key定一套命名规则 ,例如

项目名称.组名.功能名=配置值
org.team.tfunction=0001

这种方式来尽量减少与系统配置信息的冲突! 
  同时,我们也可以使用下面这种配置方式进行配置,这里我配NEVER的意思是不读取系统配置信息。如果

<context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties" 
    system-properties-mode
="NEVER"/>
原文地址:https://www.cnblogs.com/alsf/p/9326763.html