如果你在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. 使用Spring标签库

    A.Spring标签库 Web项目若使用Spring Web MVC并使用JSP作为表现的话.从Spring2.0版本开始提供一套标签库可供使用. 使用标签库无非是易于开发,维护之类云云.这里就不阐述 ...

  2. Away3D带你360°漫游全景影像

    1代码展示 package { import away3d.containers.View3D; import away3d.controllers.HoverController; import a ...

  3. cocos2d-x游戏开发系列教程-中国象棋06-游戏规则

    前情回顾 上一个博文我们提到象棋运动的函数dealWithChess,但是只是说该函数完成了棋子的选择和移动功能 其实在这个函数里,在移动棋子之前,是要对棋子的移动是否合法进行判断的,我们一起来看看如 ...

  4. JAVA之等号、传类对象参数与c++的区别

    在JAVA中用等号对类对象进行赋值,实际上操作的是对象的地址. eg: package MyText; class ClassA { int value; public void seta(int v ...

  5. asp.net后台对前台脚本的调用

    后台像前台输出要执行的脚本 Response.Write("<script>alert('内容')</script>"); asp.net后台调用前台js脚 ...

  6. Swift - 使用NSURLSession同步获取数据(通过添加信号量)

    过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据.从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSessio ...

  7. Join的实现步骤 以及连接的概念

    Join的实现步骤  以及连接的概念 我们常说连接有三种,即 交叉连接.内连接.外连接,这三者的概念很容易模糊,现在我们先放下概念,搞清楚完整连接实现的步骤: 一个完整的连接有三个步骤:.做笛卡儿积: ...

  8. Webfrom 生成流水号 组合查询 Repeater中单选与复选控件的使用 JS实战应用

                                             Default.aspx 网页界面 <%@ Page Language="C#" AutoE ...

  9. 不是什么时候都可以用栈来声明对象并使用(自动释放)——Delphi里到处都是编译器魔法,并且自动帮助实例化界面元素指针

    一直都喜欢这样显示窗口,因为简单高效: void MainWidget::ShowMyWindow() { MyWidget form(this); form.resize(,); form.exec ...

  10. 使用Boost.PropertyTree处理XML、JSON和INI数据

    Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的.目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0. 主要作用/应用场合 B ...