zoukankan      html  css  js  c++  java
  • Spring的PropertyPlaceholderConfigurer

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

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

    Spring中提供着一个PropertyPlaceholderConfigurer这个类是BeanFactoryPostProcessor的子类。

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

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
            <list> <!-- 配置jdbc -->
              <value>classpath:properties/cfg.properties</value>
          <value>classpath:properties/cfg.properties</value>

    </list>
         </property>
    </bean>

    等价于

    <context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/>

    使用PropertyPlaceholderConfigurer在XML文件中用${key}替换指定的properties文件中的值。

    https://blog.csdn.net/blueboz/article/details/54808915

  • 相关阅读:
    创建用户中遇到的问题
    创建用户和用户组
    ActiveMq在linxu系统上的启动
    CentOs 6.5 安装jdk
    卸载CentOs6.5自带的OpenJDK
    centos 6.5 添加ip地址
    CentOs 6.5的安装
    jna的简单测试
    电脑硬盘空间怎么越来越小
    build path contains duplicate entry:'src' for project 'XXX'
  • 原文地址:https://www.cnblogs.com/menbo/p/10211282.html
Copyright © 2011-2022 走看看