Spring导入多个独立的 .properties配置文件

在使用spring的时候如果在多个xml文件中配置了

<context:property-placeholder location=""/>

在控制台会打印警告,导致程序无法运行

如下面我在使用mybatis和redis的时候

spring-mybatis.xml

<context:property-placeholder location="classpath:data/mybatis.properties" />

spring-redis.xml

<context:property-placeholder location="classpath:data/redis.properties" />

导致下面异常

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'jedisPoolConfig' defined in file [C:\Users\abc\Desktop\owowl\target\classes\spring\spring-redis.xml]: Could not resolve placeholder 'redis.pool.maxTotal' in string value "${redis.pool.maxTotal}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.pool.maxTotal' in string value "${redis.pool.maxTotal}"
--------------
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.pool.maxTotal' in string value "${redis.pool.maxTotal}"

上网找这个的解决方案,找了好久终于找到一篇
http://my.oschina.net/tinglanrmb32/blog/409115?p=1

内容如下

spring中 context:property-placeholder 导入多个独立的 .properties配置文件?

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代 PropertyPlaceholderConfigurer了)。

换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。

拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer, 因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个 PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题

所以最后spring-在mvc.xml配置加载全部配置文件

    <!-- 加载所有配置文件 -->
    <context:property-placeholder location="classpath*:data/*.properties"/>
    <!-- 还可以是下面方式,加载多个目录中的 -->
    <context:property-placeholder location="classpath:*.properties,classpath:*/*.properties" /> 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值