• Spring PropertyPlaceholderConfigurer类载入外部配置


      通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer来简化代码,例如:

    <bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">  
        <property name="name" value="zero"/>  
        <property name="gender">  
            <value>man</value>  
        </property>  
    </bean>  

    我们希望将name、gender的值写在配置文件中,以后的改动只需要对配置文件进行修改即可,于是就会用到PropertyPlaceholderConfigurer:
    test.properties

    name=zero  
    gender=man

    beans.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <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-4.0.xsd">  
      
        <bean id="propertyConf" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="location" value="test.properties"/>  
        </bean>  
          
        <bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">  
            <property name="name" value="${name}"/>  
            <property name="gender">  
                <value>${gender}</value>  
            </property>  
        </bean>  
      
    </beans>  

    这里可以更简化我们的配置,使用<context:property-placeholder/>,它会自动加载PropertyPlaceholderConfigurer这个Bean(详细请看源码),于是就有了如下的配置:

    <?xml version="1.0" encoding="UTF-8"?>  
    <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.xsd">  
      
        <context:property-placeholder location="test.properties"/>  
      
        <bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">  
            <property name="name" value="${name}"/>  
            <property name="gender">  
                <value>${gender}</value>  
            </property>  
        </bean>  
    </beans>  


    <context:property-placeholder/>其它参数如下:

    <context:property-placeholder     
            location="属性文件,多个之间逗号(,)分隔"    
            file-encoding="文件编码"    
            ignore-resource-not-found="是否忽略找不到的属性文件,默认false,即不忽略,找不到将抛出异常"    
            ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常"    
            properties-ref="本地Properties配置"    
            local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反"    
            system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,OVERRIDE类似于ENVIRONMENT"    
            order="顺序,当配置多个<context:property-placeholder/>时的查找顺序"    
            />   

    转载于:https://my.oschina.net/zjllovecode/blog/1791907

  • 相关阅读:
    Windows之shortcut
    VHDL之code structure
    Embedded之Makefile
    VHDL_LIB之DFF
    VCS之Git
    潭州课堂25班:Ph201805201 django框架 第五课 自定义简单标签,包含标签,模型类创建,梳理类创建 (课堂笔记)
    潭州课堂25班:Ph201805201 django框架 第四课 模板常用标签,模板继承与引用,自定义过渡器 (课堂笔记)
    潭州课堂25班:Ph201805201 django框架 第三课 模板路径,变量,过滤器,静态文件的引用 (课堂笔记)
    潭州课堂25班:Ph201805201 django框架 第二课 url,,include,kwargs,name的使用 (课堂笔记)
    潭州课堂25班:Ph201805201 django框架 第一课 环境搭建 (课堂笔记)
  • 原文地址:https://www.cnblogs.com/twodog/p/12137148.html
Copyright © 2020-2023  润新知