简体   繁体   中英

Mixed PropertySourcesPlaceholderConfigurer and Spring bean with type java.util.properties

I have these configuration below on my application context 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>

I got the below exception when loading my context

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0' defined in class path resource [dbaccessContext.xml]: Unsatisfied dependency expressed through bean property 'properties': : No qualifying bean of type [java.util.Properties] is defined: expected single matching bean but found 2: jmsProperties,systemProperties ; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [java.util.Properties] is defined: expected single matching bean but found 3: jmsProperties,systemProperties

Appreciate for any help on this.

Updated.

I found the root cause that make my context failed to load, because my context had: default-autowire="byType" so Spring will try to inject all my spring property by type.

<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"**>

After removing default-autowire="byType" , my app work now.

If you read the exception message more carefully it already tells you com.alu.ov.ngnms.properties .PropertySourcesPlaceholderConfigurer#0 which is not a Spring class, has a properties field which is being injected.

Unfortunately, the code did not anticipate there could be more than one Properties bean in the application context. You'll have to modify that (anoymous inner) class and explicitly specify the Properties bean it was originally expecting by name (either with @Named or @Resource depending on your code convention).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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