Spring属性文件读取——PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer
介绍
PropertyPlaceholderConfigurer是bean工厂后置处理器的实现,即BeanFactoryPostProcessor接口的一个实现。
作用
在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码。
PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件进行修改,而不用对xml配置文件进行修改
使用方法
编辑properties文件
例如以下目录结构:

在.properties中可以编辑配置信息
jdbc.url=jdbc:mysql://178.57.208.86:3306/ds?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
jdbc.username=root
jdbc.password=*****
在.xml中引入外部文件,即.properties文件
单个文件引入:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>jdbc.properties</value>
<

本文介绍了Spring中的PropertyPlaceholderConfigurer,它是BeanFactoryPostProcessor接口的实现,用于在XML配置文件中引用外部属性文件。内容包括PropertyPlaceholderConfigurer的作用、使用方法,如编辑properties文件、引入外部文件、配置使用,以及Spring简化使用的方法。同时,文章讨论了自定义PropertyPlaceholderConfigurer的场景,通过继承并重写processProperties方法以满足特定需求。
最低0.47元/天 解锁文章
1674

被折叠的 条评论
为什么被折叠?



