Spring使用context:property-placeholder导入多个路径下properties文件

本文详细解析了Spring配置文件中<context:property-placeholder>标签的使用方法,包括单实例限制、属性覆盖原则及classpath与classpath*的区别。阐述了如何正确配置属性文件路径,确保应用正确加载外部配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<context:property-placeholder location="classpath*:apppcconf/conf/*.properties" />

<context:property-placeholder location="classpath*:db-sys.properties" />


同时写两个<context:property-placeholder>标签在Spring配置文件中不行,只能存在一个,Spring通过反射扫描标签的命名实例化Bean。
org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer只能存在一个实例。

 

<context:property-placeholder location="classpath*:db-sys.properties,classpath*:apppcconf/conf/*.properties" />


如果有相同属性名,多个文件只会取最后读取的一个


classpath和classpath*区别:

classpath:只会到你的class路径中查找找文件。

classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。

注意: 用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。

classpath*的使用:

当项目中有多个classpath路径,并同时加载多个classpath路径下(此种情况多数不会遇到)的文件,*就发挥了作用,如果不加*,则表示仅仅加载第一个classpath路径。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值