【问题标题】:Mixed PropertySourcesPlaceholderConfigurer and Spring bean with type java.util.propertiesjava.util.properties 类型的混合 PropertySourcesPlaceholderConfigurer 和 Spring bean
【发布时间】:2015-09-30 08:41:03
【问题描述】:

我在我的应用程序上下文 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 时出错 'org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0' 在类路径资源 [dbaccessContext.xml] 中定义:不满意 通过 bean 属性“properties”表示的依赖关系::否 [java.util.Properties] 类型的限定 bean 已定义:预期 单个匹配 bean 但找到 2: jmsProperties,systemProperties;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException: 否 [java.util.Properties] 类型的限定 bean 已定义:预期 单个匹配的 bean,但找到了 3 个: jmsProperties,系统属性

感谢您对此的任何帮助。

已更新。

我找到了导致我的上下文加载失败的根本原因,因为我的上下文有: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" 后,我的应用现在可以工作了。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    如果您仔细阅读异常消息,它已经告诉您 <strong>com.alu.ov.ngnms.properties</strong>.PropertySourcesPlaceholderConfigurer#0 不是 Spring 类,有一个正在注入的 properties 字段。

    不幸的是,代码没有预料到应用程序上下文中可能有多个Properties bean。您必须修改该(匿名内部)类并通过名称明确指定它最初期望的 Properties bean(根据您的代码约定使用 @Named@Resource)。

    【讨论】:

    • 这是我的错字,我更新了。我的代码只有上下文引用 dbaccessContext.xml
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2021-05-17
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多