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

望大家多多指点。

PropertyPlaceholderConfigurer类的使用注意的更多相关文章

  1. Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)

    一.PropertyPlaceholderConfigurer类的作用 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFact ...

  2. spring读取配置文件PropertyPlaceholderConfigurer类的使用

    这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...

  3. Spring里PropertyPlaceholderConfigurer类的使用

    1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...

  4. org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类

    <bean id="investorQueryConfigurer" class="org.springframework.beans.factory.config ...

  5. Spring PropertyPlaceholderConfigurer类载入外部配置

    2019独角兽企业重金招聘Python工程师标准>>> 通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.co ...

  6. Spring常用的接口和类(二)

    七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...

  7. spring中propertyplaceholderconfigurer简介

    Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...

  8. Spring常用接口和类

    一.ApplicationContextAware接口 当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口.代码展示如下: p ...

  9. PropertyPlaceholderConfigurer

    PropertyPlaceholderConfigurer Spirng在生命周期里关于Bean的处理大概可以分为下面几步: 加载 Bean 定义(从xml或者从@Import等) 处理 BeanFa ...

随机推荐

  1. Android 判断当前线程是否为主线程

    public static boolean isInMainThread() { return Looper.myLooper() == Looper.getMainLooper(); }

  2. Spring Thread Pool 线程池的应用

    Spring and Java Thread example 扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Download it ...

  3. JAVA_2Lesson

    package test; public class abc { public static void main(String[] arg) { int[][] xx=new int[3][]; xx ...

  4. 教你看懂C++类库函数定义之一---HRESULT 宏

    一切从一个C++ 类库头文件开始,现在在做一个C++的项目,期间用到一个开源的界面库DUILib(类似MFC),这个东西还不错能很容易的写出漂亮的界面,比如QQ的界面,可以去下载下来研究研究,地址:h ...

  5. .bat脚本将windows server 2008设置成ntp时间同步服务器

    @echo off echo autor OAK @echo off echo -------------------------------- @echo off REG ADD HKEY_LOCA ...

  6. SharePoint 2013的100个新功能之搜索(二)

    一:名称建议 人员搜索中新的“名称建议”功能,微软引入了一种简单.直观的方式来根据名称找到用户.输入一个或多个字符,查看全部以其开头的名称,在所有的用户描述数据库都可用,在人员索引中也因此一样可用.该 ...

  7. 编写一个void sort(int*x,int n)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件

    #include<stdio.h> void sort(int*x,int n) { int i,j,k,t; for(i=0;i<n-1;i++) { k=i; for(j=i+1 ...

  8. 静态代码检查工具 cppcheck 的使用(可分别集成到VS和QT Creator里)

    CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们写的 ...

  9. 最新OpenCV2.4.6与VS2010开发环境搭建

    OpenCV2.4.6与VS2010开发环境搭建 由于很久没有用OpenCV了,之前用的是1.0版本和VC++6.0.现在已经到了VS2010+OpenCV2.4.6.安装使用之后,发现OpenCV的 ...

  10. Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边

    Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边,当本地有图片的时候,直接从本地读取图片,如果本地没有图片,将从服务器异步加载图片 package com.example. ...