繁体   English   中英

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

[英]Mixed PropertySourcesPlaceholderConfigurer and Spring bean with type java.util.properties

我在我的应用程序上下文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:在类路径资源[dbaccessContext.xml]中定义的名称为'org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0'的bean创建时出错:通过bean属性'properties'表达的不满意依赖没有定义类型为[java.util.Properties]的合格Bean:预期的单个匹配Bean,但找到了2:jmsProperties,systemProperties 嵌套的异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义类型为[java.util.Properties]的合格bean:期望的单个匹配bean,但发现3:jmsProperties,systemProperties

感谢对此的任何帮助。

更新。

我发现导致我的上下文无法加载的根本原因是因为我的上下文具有:default-autowire =“ byType”,因此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 =“ byType”后 ,我的应用程序现在可以工作了。

如果您更仔细地阅读异常消息,则该异常消息已经告诉您com.alu.ov.ngnms.properties .PropertySourcesPlaceholderConfigurer#0 (不是Spring类)具有正在插入的properties字段。

不幸的是,代码没有预期应用程序上下文中可能有多个Properties Bean。 您必须修改该(异常内部)类,并按名称显式指定其最初期望的Properties Bean(根据您的代码约定,使用@Named@Resource )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM