混合PropertySourcesPlaceholderConfigurer和Spring bean,类型为java.util.properties

时间:2015-07-11 18:26:36

标签: java spring

我的应用程序上下文xml

下面有这些配置
  <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jmsconfig.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="order" value="1"/>
    </bean>

    <bean id="jmsConfigPropertyPlaceHolder" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">           
        <property name="locations">
            <list>
                <bean class="org.springframework.core.io.FileSystemResource">
                    <constructor-arg value="#{systemEnvironment['SHARED_DIR']}/messaging/broker.properties" />
                </bean>         
            </list>
        </property>    
    </bean>

    <bean id="jmsProperties" class="java.util.Properties">
        <constructor-arg>
            <props>
                <prop key="transportURI">${transportURI}</prop>
                <prop key="maxConcurrentConsumers">${maxConcurrentConsumers}</prop>
                <prop key="timeToLive">${timeToLive}</prop>
                <prop key="cacheConsumer">${cacheConsumer}</prop>
                <prop key="cacheProducer">${cacheProducer}</prop>
                <prop key="deliveryPersistent">${deliveryPersistent}</prop>
            </props>
        </constructor-arg>
    </bean>

加载我的上下文时我得到了以下异常

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   使用名称创建bean时出错   &#39; org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0&#39;   在类路径资源[dbaccessContext.xml]中定义:不满意   通过bean属性表达的依赖性&#39; :: 否   定义[java.util.Properties]类型的限定bean:expected   单匹配bean但找到2:   jmsProperties,systemProperties ;嵌套异常是   org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有   定义[java.util.Properties]类型的限定bean:expected   单匹配bean但发现3:   jmsProperties,systemProperties

感谢您的任何帮助。

更新

我找到了导致我的上下文无法加载的根本原因,因为我的上下文有:default-autowire =&#34; byType&#34;所以Spring会尝试按类型注入我所有的spring属性。

<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:aop="http://www.springframework.org/schema/aop" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    **default-autowire="byType"**>

删除 default-autowire =&#34; byType&#34; 后,我的应用现在正常工作。

1 个答案:

答案 0 :(得分:0)

如果您更仔细地阅读了异常消息,它已经告诉您com.alu.ov.ngnms.properties.PropertySourcesPlaceholderConfigurer#0这不是Spring类,那么正在注入一个properties字段。

不幸的是,代码没有预料到应用程序上下文中可能有多个Properties bean。你必须修改那个(anoymous inner)类并明确指定它最初期望的Properties bean(根据你的代码约定,@Named@Resource。< / p>