zoukankan      html  css  js  c++  java
  • PropertyPlaceholderConfigurer类的使用注意

    如果你在spring的applicationcontext.xml中需要使用属性配置文件,那PropertyPlaceholderConfigurer这个类就是必须的。

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
    <value>classpath:conf/setting.properties</value>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreUnresolvablePlaceholders" value="true"></property>
    </bean>

    假设如果你仅仅就需要一个属性文件就没什么了,但如果你需要两个配置文件,并且两个配置文件里都有name属性。这里假设setting.properties属性文件

    里有name=hello,同时setting_new.properties里面也有name=xiaoQ。然后我又加个PropertyPlaceholderConfigurer,那么我们就看看完整的信息

    <?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-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <bean id="mockAnno" class="org.mm.annoTest.MockTestInterfaceImpl"/>
    <bean id="impls" class="org.mm.wind.HandlerInterfaceImpl"/>

    <bean id="s3" class="org.mm.wind.S3impl">
    <property name="names" value="${name}"/>
    </bean>

    <bean id="handler" class="org.mm.anno.MockWiredHandler"/>
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
    <value>classpath:conf/setting.properties</value>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreUnresolvablePlaceholders" value="true"></property>
    </bean>
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
    <value>classpath:conf/setting_new.properties</value>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreUnresolvablePlaceholders" value="true"></property>
    </bean>

    </beans>

    这样的配置会有什么结果。结果就是name=hello

    而程序的执行流程是怎么样的呢?可能有人会觉得初始化的时候会把properties文件加载到内存然后再初始化bean的属性。

    当然我开始的时候也是那么认为。但如果是这样的话,那么后来加载的这个属性问价的那么就会override先加载的name,但

    这个和我们的结果却不相匹配。于是debug下。说下这个加载的流程吧:

    加载第一个PropertyPlaceholderConfigurer类的时候则扫描所有的bean然后把能赋值属性的都给赋值,当加载第二个属性配置文件

    的时候则再次扫描所有的bean然后把能赋值属性的都给赋值。所以这里我们先加载了第一个配置文件的时候就已经给name赋值了。

    当第二次加载setting_new.properties这个属性文件的时候。根本就没有属性可以赋值了。

    望大家多多指点。


  • 相关阅读:
    [转]maven for eclipse在线安装 eclipsesr2
    js循环绑定事件解决方案
    设置 Eclipse/ 快速提示快捷键
    [转]POI 读取 Excel 转 HTML 支持 03xls 和 07xlsx 版本 包含样式
    解决子元素和父元素同时触发onclick
    【Tomcat】本地域名访问配置
    [ELK]快速搭建简单的日志分析平台
    Git 使用心得
    无光驱U盘启动WinPE安装操作系统的方法
    WMI调用发生 InitializationFailure 错误的解决过程
  • 原文地址:https://www.cnblogs.com/riskyer/p/3312927.html
Copyright © 2011-2022 走看看