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这个属性文件的时候。根本就没有属性可以赋值了。

    望大家多多指点。


  • 相关阅读:
    妙用Telnet快速收发电子邮件(转载)
    windows server 2003如何安装IIS,配置IIS,让iis支持aspx(收集)
    T7400等DELL工作站及服务器的Windows server 2003系统安装——解决“找不到安装在计算机上的硬盘驱动器 安装无法继续,要退出请按F3”问题
    PostgresSQL连接认证设置(收集)
    安装PostgreSQL :Problem running postinstall (收集)
    UltiDev Cassini Web Server介绍
    配置Lumisoft Mail Server给外网邮箱发消息
    mysql命令行常用命令(收集)
    SQL复制数据表及表结构
    解决lumisoft mail server使用中的错误“550 5.7.1 Unable to relay for xxx”
  • 原文地址:https://www.cnblogs.com/riskyer/p/3312927.html
Copyright © 2011-2022 走看看