一、PropertyPlaceholderConfigurer 的继承体系

PropertyPlaceholderConfigurer位于org.springframework.beans.factory.config 包下,它的继承体系如下

PropertyPlaceholderConfigurer 直接继承于PlaceholderConfigurerSupport,它的已知实现类只有一个

PreferencesPlaceholderConfigurer

二、PropertyPlaceholderConfigurer 的基本概念

​ 源自JavaDoc: PropertyPlaceholderConfigurer 是 PlaceholderConfigurerSupport 的一个子类,用来解析${…} 占位符的,可以使用setLocationsetProperties设置系统属性和环境变量。从Spring3.1 开始,PropertySourcesPlaceholderConfigurer应优先与此实现,通过使用Spring3.1 中的 EnvironmentPropertySource机制, 使它的灵活性更强。

​ 但是PropertyPlaceholderConfigurer却适用如下情况:当 spring-context 模块不可用的时候,使用BeanFactory的API 而不是ApplicationContext的API。现有配置使用setSystemPropertiesModesetSystemPropertiesModeName属性,建议用户不要使用这些设置, 而是使用容器的Environment属性;

在Spring3.1 之前,<context:property-placeholder/>命名空间保存了PropertyPlaceholderConfigurer的实例,如果使用spring-context-3.0 xsd的定义的话,仍然会这样做。也就是说,即使使用Spring 3.1,您也可以通过命名空间保留PropertyPlaceholderConfigurer; 只是不更新schemaLocation 并继续使用3.0 XSD。

三、PropertyPlaceholderConfigurer 的基本使用

  1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${...}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
  2. 在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件

PropertyPlaceholderConfigurer 引入外部属性文件

  • 定义一个properties 属性文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sys
jdbc.username=root
jdbc.password=123456

这是一个最基本的配置数据库连接的设置,前缀统一使用jdbc来命名

  • 定义xml用来获取上面properties中的内容
    <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-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> </beans>

通过给PropertyPlaceholderConfigurer 设置一个bean,指定的名称为location,指定value值就能够引入外部配置文件,然后就能够通过${jdbc.key} 来获取properties 中的值

PropertyPlaceholderConfigurer 引入多个属性文件

  • 再来定义一个encoding.properties
file.encoding=utf-8
file.name=encoding
  • PropertyPlaceholderConfigurer 引入多个属性文件比较简单,需要把location -> locations ,然后直接指定一个list 就能够引入
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>database.properties</value>
<value>encoding.properties</value>
</list>
</property>
</bean>
  1. 这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
  2. 查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

PropertyPlaceholderConfigurer 的替代方案

​ 正如PropertyPlaceholderConfigurer基本概念中提到的,Spring可以使用<context:property-placeholder/> 作为PropertyPlaceholderConfigurer 的替代方案,代码如下

    <!-- 指定单个properties -->
<!--<context:property-placeholder location="database.properties" />-->
<!-- 指定多个properties-->
<!--<context:property-placeholder location="classpath:*.properties"/>-->
<!--<context:property-placeholder location="classpath:database.properties, classpath:encoding.properties"/>-->
<!-- 指定配置文件加载顺序-->
<context:property-placeholder order="0" location="database.properties" />
<context:property-placeholder order="1" location="encoding.properties" />

四、自定义PropertyPlaceholderConfigurer

  • 自定义一个SubPropertyPlaceholderConfigurer 继承自PropertyPlaceholderConfigurer
    public class SubPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

        private static Map<String, String> ctxPropertiesMap;

        @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
// 调用父类PropertyPlaceholderConfigurer 的构造器
super.processProperties(beanFactoryToProcess, props);
// 遍历配置文件的key,Properties 对象就是导入的配置文件
Enumeration<?> enumeration = props.propertyNames();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
} ctxPropertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
} public static String getProperty(String name){
return ctxPropertiesMap.get(name);
}
}
  • 需要引入这个自定义的SubPropertyPlaceholderConfigurer
    <bean id="propertyPlaceholderConfigurer" class="com.cxuan.spring.common.SubPropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

如何启动呢?其实引入的SubPropertyPlaceholderConfigurer 就能够随着Spring加载配置文件而被加载。

直接定义main方法,用ClassPathXmlApplicayionContext引入任意的配置文件即可。

文章参考:

Spring里PropertyPlaceholderConfigurer类的使用](https://www.cnblogs.com/huqianliang/p/5673701.html)

PropertyPlaceholderConfigurer读取配置文件

https://blog.csdn.net/y_index/article/details/79893765

https://blog.csdn.net/eson_15/article/details/51365707

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html#processProperties-org.springframework.beans.factory.config.ConfigurableListableBeanFactory-java.util.Properties-

https://blog.csdn.net/wrs120/article/details/84554366

PropertyPlaceholderConfigurer 基本用法的更多相关文章

  1. PropertyPlaceholderConfigurer的用法:

    用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  2. spring读取数据库的配置信息(url、username、password)时的<bean>PropertyPlaceholderConfigurer的用法

    用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  3. PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)

    http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)

  4. Spring注解?啥玩意?

    目录 基础概念:@Bean 和 @Configuration 使用AnnotationConfigApplicationContext 实例化Spring容器 简单的构造 使用register注册IO ...

  5. Spring PropertyPlaceholderConfigurer占位符用法

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

  6. PropertiesFactoryBean PropertyPlaceholderConfigurer 区别

    正如 stackoverflow上说的,PropertiesFactoryBean 是PropertiesLoaderSupport 直接的实现类, 专门用来管理properties文件的工厂bean ...

  7. async4j 普通用法、整合spring用法

    1.普通用法 asyn4j 是一个java异步方法调用框架,基于消费者与生产者模式. async4j就是基于Executors线程池和反射机制实现的. 包括了异步方法执行,异步回调执行,异步工作缓存模 ...

  8. MyBatis(七):mybatis Java API编程实现增、删、改、查的用法

    最近工作中用到了mybatis的Java API方式进行开发,顺便也整理下该功能的用法,接下来会针对基本部分进行学习: 1)Java API处理一对多.多对一的用法: 2)增.删.改.查的用法: 3) ...

  9. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

随机推荐

  1. 循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案

    在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的id删除用户信息.我把用户信息从文件提取出来储存在了字典里,其中key是用户id,value是用户的其他信息.在循环字典的时候,当用户id和 ...

  2. ARM系统调用

    参考:Linux异常处理体系结构 linux系统调用表(system call table)  Arm Linux系统调用流程详细解析-SWI ARM系统调用是通过SWI异常处理函数实现的,这里简要概 ...

  3. Erasing and Winning UVA - 11491 贪心

    题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ...

  4. 库函数的使用:POJ1488-TEX Quotes(getline()的使用)

    TEX Quotes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9385 Description TEX is a type ...

  5. poj 1321 排兵布阵问题 dfs算法

    题意:有不规则地图,在上面放n个相同的棋子,要求摆放的时候不同行不同列.问:有多少种摆法? 思路:dfs+回溯 用一个book[]数组来表示当前列是否有放棋子 一行一行的遍历,对一行来说遍历它的列,如 ...

  6. MIP启发式算法:遗传算法 (Genetic algorithm)

    *本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 本文主要讲述启发式算法中的遗传算法.遗传算法也是以local search为核心框架,但在表现形式上和hill climbing, ta ...

  7. Spring boot 中Spring data JPA的应用(一)

    最近一直在研究Spring Boot,今天为大家介绍下Spring Data JPA在Spring Boot中的应用,如有错误,欢迎大家指正. 先解释下什么是JPA JPA就是一个基于O/R映射的标准 ...

  8. Linux学习-Shell的变量功能

    什么是变量? 简单的说,就是让某一个特定字串代表不固定的内容. 变量的可变性与方便性 举例来说,我们每个帐号的邮件信箱默认是以 MAIL 这个变量来进行存取的, 当 dmtsai 这个 使用者登陆时, ...

  9. HDU 4812 D Tree 树分治

    题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...

  10. 并查集 - BZOJ 1104 [POI2007]洪水

    BZOJ 1104 [POI2007]洪水 描述 AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了他的所有顾问(包括你 ...