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这个属性文件的时候。根本就没有属性可以赋值了。
望大家多多指点。
PropertyPlaceholderConfigurer类的使用注意的更多相关文章
- Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)
一.PropertyPlaceholderConfigurer类的作用 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFact ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- Spring里PropertyPlaceholderConfigurer类的使用
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类
<bean id="investorQueryConfigurer" class="org.springframework.beans.factory.config ...
- Spring PropertyPlaceholderConfigurer类载入外部配置
2019独角兽企业重金招聘Python工程师标准>>> 通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.co ...
- Spring常用的接口和类(二)
七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...
- spring中propertyplaceholderconfigurer简介
Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...
- Spring常用接口和类
一.ApplicationContextAware接口 当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口.代码展示如下: p ...
- PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer Spirng在生命周期里关于Bean的处理大概可以分为下面几步: 加载 Bean 定义(从xml或者从@Import等) 处理 BeanFa ...
随机推荐
- java--匿名类
匿名类的使用 package Test; abstract class C525{ abstract void foo(); } class B525{ // 局部类只能访问外包方法中的final成员 ...
- ls命令显示可执行的文件 ls -F
ls命令显示可执行的文件 ls -F
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables opt
mysql跳过权限: mysqld -nt --skip-grant-tables opt 登录:mysql -uroot -p 修改root密码 set password for 'root'@'l ...
- 集群安装配置Hadoop具体图解
集群安装配置Hadoop 集群节点:node4.node5.node6.node7.node8. 详细架构: node4 Namenode,secondnamenode,jobtracker node ...
- 创建服务类PO
转载:https://blogs.sap.com/2014/03/04/creating-a-simple-service-po-using-bapipocreate1bapipochange/ Cr ...
- 用js模拟struts2的多action调用
近期修了几个struts2.1升级到2.3后动态方法调用失效的bug,深有感悟, 原始方法能够參考我之前的博文:struts2.1升级到2.3后动态调用方法问题 可是我那种原始方法有一个局限,就是在s ...
- opencv之haar特征+AdaBoos分类器算法流程(二)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)
想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...
- 微软Ajax--UpdatePanel控件
今天用做日历显示本月的考勤记录,用到了UpdatePanel控件,才发现对这个控件并不太了解,所以找了点儿资料,整理了一下给大家发上来! 一.UpdatePanel的结构 <asp:Script ...
- Managing your Actor Systems
今天偶然得机会,找到一篇不错得文章,现在分享给大家. 原文:http://www.kotancode.com/2013/02/15/managing-your-actor-systems/ If yo ...