spring同时集成遇到多个properties资源文件加载的问题

来源:互联网 发布:h5单页商品详情源码 编辑:程序博客网 时间:2024/06/09 14:34

这两天平台中集成redis和mongodb遇到一个问题

单独集成redis和单独集成mongodb时都可以正常启动程序,但是当两个同时集成进去时就会报以下问题

 

Java代码  收藏代码
  1.    
  2. Could not resolve placeholder 'mongo.port' in string value "${mongo.port}  

 

 百思不得解后,经多方搜集查证,终于找到问题原因。

在spring的xml配置文件中当有多个*.properties文件需要加载时。

应该这样使用使用

 

Java代码  收藏代码
  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="locations">  
  3.         <list>  
  4.             <value>classpath*:mongodb.properties</value>  
  5.         </list>  
  6.     </property>  
  7.     <property name="ignoreUnresolvablePlaceholders" value="true" />   
  8. </bean>  

 或者

 

Java代码  收藏代码
  1. <context:property-placeholder location="classpath*:redis.properties" ignore-unresolvable="true" />  

 但是 ignore-unresolvable="true" 和 <property name="ignoreUnresolvablePlaceholders" value="true" /> 这两个属性值必须为true

 

 

原因如下(摘自于文章最后的链接)

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

而<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。 

 

 原文章中提到最后是把所有的资源文件中的资源放在一起加载

如下:

 

Java代码  收藏代码
  1. #mongo的资源属性  
  2. mongo.host=192.168.111.230  
  3. mongo.port=40000  
  4. mongo.connectionsPerHost=8  
  5. mongo.threadsAllowedToBlockForConnectionMultiplier=4  
  6. mongo.connectTimeout=1500  
  7. mongo.maxWaitTime=1500  
  8. mongo.autoConnectRetry=true  
  9. mongo.socketKeepAlive=true  
  10. mongo.socketTimeout=1500  
  11. mongo.slaveOk=true  
  12. mongo.write.number=1  
  13. mongo.write.timeout=0  
  14. mongo.write.fsync=true  
  15.   
  16. mongo.dbname=test  
  17.   
  18. #redis的资源属性  
  19. redis.host=192.168.111.225    
  20. redis.port=6379    
  21. redis.pass=    
  22.     
  23. redis.maxIdle=300  
  24. redis.maxTotal=600  
  25. redis.minIdle=100    

 但是本人认为这样加载不利于系统的拆分,耦合较高。因此本人推荐还是使用单独加载每个子系统自己的资源文件最好,如:

Java代码  收藏代码
  1. #mongo加载资源文件  
  2.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <list>  
  5.                 <value>classpath*:mongodb.properties</value>  
  6.             </list>  
  7.         </property>  
  8.         <property name="ignoreUnresolvablePlaceholders" value="true" />   
  9.     </bean>  
  10.   
  11. #redis加载资源文件  
  12.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  13.         <property name="locations">  
  14.             <list>  
  15.                 <value>classpath*:redis.properties</value>  
  16.             </list>  
  17.         </property>  
  18.         <property name="ignoreUnresolvablePlaceholders" value="true" />   
  19.     </bean>  

 

 只要保证ignoreUnresolvablePlaceholders都为true,或这最后一个加载的为false,之前的都为true即可。

0 0
原创粉丝点击