Spring xml读取properties相关问题

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

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

单独集成mysql和单独集成mongodb时都可以正常启动程序,但是当两个同时集成进去时就会报以下问题
Could not resolve placeholder ‘mongo.port’ in string value “${mongo.port}
在spring的xml配置文件中当有多个*.properties文件需要加载时。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 开启IOC注解扫描 -->
    <context:component-scan  base-package="com"/>
    <!-- 启用spring mvc注解 -->
    <context:annotation-config />


     <!--第一种方式将多个配置文件读取到容器中,交给Spring管理-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
                <value>classpath:/properties/*.properties</value>
                <!--<value>classpath:/properties/mongodb.properties</value>-->
                <!--<value>classpath:/properties/jdbc.properties</value>-->
            </list>
        </property>
    </bean>

    <!--第二种方式-->
    <!--<context:property-placeholder location="classpath*:*.properties" ignore-unresolvable="true"/>-->
    <import resource="classpath:/conf/spring-mvc.xml" />

    <import resource="classpath:/conf/spring-ehcache.xml" />

    <import resource="classpath:/conf/spring-mybatis.xml" />

    <import resource="classpath:/conf/mongodb-context.xml" />
</beans>

原因如下:

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

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

关于:

ignore-unresolvable="true" 和 <property name="ignoreUnresolvablePlaceholders" value="true" />

这两个属性值必须为true

同个模块中如果出现多个context:property-placeholder ,location properties文件后,运行时出现Could not resolve placeholder ‘key’ in string value keycontext:propertyplaceholderbeanbeancontext:propertyplaceholderproperties k e y 。 原 因 是 在 加 载 第 一 个 c o n t e x t : p r o p e r t y − p l a c e h o l d e r 时 会 扫 描 所 有 的 b e a n , 而 有 的 b e a n 里 面 出 现 第 二 个 c o n t e x t : p r o p e r t y − p l a c e h o l d e r 引 入 的 p r o p e r t i e s 的 占 位 符 {key},此时还没有加载第二个property-placeholder,所以解析不了${key}。解决办法一,可以将通过模块的多个property-placeholder合并为一个,将初始化放在一起。方法二,添加ignore-unresolvable=”true”,这样可以在加载第一个property-placeholder时出现解析不了的占位符进行忽略掉。

如果java代码中引用properties中的值,可以用标签引用;

  @Value("${student.name}") 
  private String name; 

或者

    @Value("#{configProperties['api.user']}")
    private String user;

推荐第二种,第一种方式可能因为加载顺序问题取不到值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值