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

    望大家多多指点。


  • 相关阅读:
    并发编程与高并发学习笔记六
    并发编程与高并发学习笔记五
    并发编程与高并发学习笔记四
    浅谈数据挖掘与机器学习
    IntelliJ Idea常用的快捷键
    单链表的插入,查找,删除
    二叉树的序列化和反序列化及完全二叉树和满二叉树的区别
    二叉树的按层遍历并实现换行
    冒泡排序,选择排序,插入排序,快速排序的比较及优化
    axSpA患者新发炎症更容易发生在既往发生过炎症的区域
  • 原文地址:https://www.cnblogs.com/riskyer/p/3312927.html
Copyright © 2011-2022 走看看