zoukankan      html  css  js  c++  java
  • 浅谈Spring的读取配置信息

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

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

      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"/>

    参考文章:

    1、浅谈Spring的PropertyPlaceholderConfigurer

  • 相关阅读:
    oracle sql日期比较
    rlwrap真是一个好东西
    Dataguard 归档丢失处理
    oracle latch工作原理
    Latch free等待事件
    Redhat Enterprise Linux 5 安装 Oracle 10g release 2
    oracle 日期问题
    Oracle自动存储管理 ASMLib的支持变化
    [官方文档] oracle官方文档总汇(9i,10g,11gR1, 11gR2)
    实时分布式搜索引擎比较(senseidb、Solr、elasticsearch)
  • 原文地址:https://www.cnblogs.com/AlanWilliamWalker/p/11122672.html
Copyright © 2011-2022 走看看