博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc加载xml文件读取本地properties配置文件
阅读量:5886 次
发布时间:2019-06-19

本文共 6002 字,大约阅读时间需要 20 分钟。

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。由这个类别,您可以将一些组态设定,移出至.properties档案中,如此的安排可以让XML定义档负责系统相关设定,而.properties档可 以作为客户根据需求,自定义一些相关的参数。实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置 信息放在bean中定义的工具。

(1)使用以下方式读取单个本地文件到xml中:

file:/opt/config/config1.properties

(2)使用以下方式读取多个本地文件到xml中:

    
file:/opt/config/config1.properties
         
file:/opt/config/config2.properties

注意读取单个配置文件,使用location,多个使用locations

上面是介绍org.springframework.beans.factory.config.PropertyPlaceholderConfigurer工具类的具体使用,下面浅析Spring框架下PropertyPlaceholderConfigurer类

package org.springframework.beans.factory.config;import java.util.Properties;import java.util.Set;import org.springframework.beans.BeansException;import org.springframework.core.Constants;import org.springframework.util.PropertyPlaceholderHelper;import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;import org.springframework.util.StringValueResolver;public class PropertyPlaceholderConfigurer extends PlaceholderConfigurerSupport {    /** 是否覆盖proprties文件 */    public static final int SYSTEM_PROPERTIES_MODE_NEVER = 0;    /**     * 默认值,不存在时覆盖     */    public static final int SYSTEM_PROPERTIES_MODE_FALLBACK = 1;    /**覆盖proprties文件*/    public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2;    private static final Constants constants = new Constants(PropertyPlaceholderConfigurer.class);    private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;    private boolean searchSystemEnvironment = true;    public void setSystemPropertiesModeName(String constantName) throws IllegalArgumentException {        this.systemPropertiesMode = constants.asNumber(constantName).intValue();    }       public void setSystemPropertiesMode(int systemPropertiesMode) {        this.systemPropertiesMode = systemPropertiesMode;    }    public void setSearchSystemEnvironment(boolean searchSystemEnvironment) {        this.searchSystemEnvironment = searchSystemEnvironment;    }       protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {        String propVal = null;        if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {            propVal = resolveSystemProperty(placeholder);        }        if (propVal == null) {            propVal = resolvePlaceholder(placeholder, props);        }        if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {            propVal = resolveSystemProperty(placeholder);        }        return propVal;    }      protected String resolvePlaceholder(String placeholder, Properties props) {        return props.getProperty(placeholder);    }      protected String resolveSystemProperty(String key) {        try {            String value = System.getProperty(key);            if (value == null && this.searchSystemEnvironment) {                value = System.getenv(key);            }            return value;        }        catch (Throwable ex) {            if (logger.isDebugEnabled()) {                logger.debug("Could not access system property '" + key + "': " + ex);            }            return null;        }    }    @Override    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)            throws BeansException {        StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);        this.doProcessProperties(beanFactoryToProcess, valueResolver);    }       @Deprecated    protected String parseStringValue(String strVal, Properties props, Set
visitedPlaceholders) { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props); return helper.replacePlaceholders(strVal, resolver); } private class PlaceholderResolvingStringValueResolver implements StringValueResolver { private final PropertyPlaceholderHelper helper; private final PlaceholderResolver resolver; public PlaceholderResolvingStringValueResolver(Properties props) { this.helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); this.resolver = new PropertyPlaceholderConfigurerResolver(props); } public String resolveStringValue(String strVal) throws BeansException { String value = this.helper.replacePlaceholders(strVal, this.resolver); return (value.equals(nullValue) ? null : value); } } private class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver { private final Properties props; private PropertyPlaceholderConfigurerResolver(Properties props) { this.props = props; } public String resolvePlaceholder(String placeholderName) { return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, props, systemPropertiesMode); } }}

 

首先看下PropertyPlaceholderConfigurer类的结构

1、注入会初始化property的location属性

 

public void setLocation(Resource location) {    this.locations = new Resource[] {location};}public void setLocations(Resource[] locations) {    this.locations = locations;}

 

2、SystemPropertiesModeName:SystemPropertiesModeName属性用来指定系统变量(System.getProperty)是否覆盖proprties文件。默认是在配置文件里找不到时使用。三个选项分别是:

 

  SYSTEM_PROPERTIES_MODE_NEVER(不覆盖)

  SYSTEM_PROPERTIES_MODE_FALLBACK(默认值,不存在时覆盖)

  SYSTEM_PROPERTIES_MODE_OVERRIDE(覆盖)

  从源码中可以看出searchSystemEnvironment属性的默认值为true,即允许环境变量覆盖properties中的值。

转载于:https://www.cnblogs.com/yantz/p/4582811.html

你可能感兴趣的文章
zipkin相关材料
查看>>
head,tail读取文件数据行
查看>>
路由相关
查看>>
weblogic启动慢,停到log manager基本不动了
查看>>
bootstrap 框架学习笔记
查看>>
XP服务详解与建议
查看>>
如何利用CSS画个桃心
查看>>
我的友情链接
查看>>
第十八章、Linux网络配置
查看>>
Linux查看系统的负载
查看>>
linux下lvm逻辑卷和快照的命令详解
查看>>
linux nginx中网页乱码
查看>>
PCA 笔记
查看>>
Exchange 2010 系列学习-安装配置
查看>>
lftp手册
查看>>
移动端常见随屏幕滑动顶部固定导航栏背景色透明度变化简单jquery特效
查看>>
matlab-线性代数 判断 向量组的线性相关性
查看>>
ubuntu 12.04下搭建web服务器(MySQL+PHP+Apache) 教程
查看>>
消息队列的应用场景、为什么要用消息队列
查看>>
IDEA同时启动两个Web项目
查看>>