Simeon
Simeon

Reputation: 7792

Why does Spring report constructor arg as ambiguous

I'm trying to init this map bean:

<bean id="totalEventCountStore" class="java.util.concurrent.ConcurrentHashMap">
    <constructor-arg type="java.util.Map">
        <map key-type="com.company.EventType" value-type="java.util.concurrent.atomic.AtomicLong">
            <entry key="ROUTED_REQUEST">
                <bean class="java.util.concurrent.atomic.AtomicLong">
                    <constructor-arg index="0" type="long" value="0"/>
                </bean>
            </entry>

.... more entries .....

</bean>

I get:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'totalEventCountStore' defined in class path resource [diagnostics.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

Does anyone know why this is happening ? ConcurrentHashMap has only one constructor that takes a Map argument.

Thanks.

Upvotes: 1

Views: 5794

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149175

I am not really sure of what your problem is, but I am sure what it is not : it is not about the ConcurrentHashMap initialization, nor about the AtomicLong.

Here is an extract of an applicationContext that is successfully loaded by Spring 3.2.4 :

<bean id="totalEventCountStore" class="java.util.concurrent.ConcurrentHashMap">
<constructor-arg type="java.util.Map">
    <map key-type="java.lang.String" value-type="java.util.concurrent.atomic.AtomicLong">
        <entry key="ROUTED_REQUEST">
            <bean class="java.util.concurrent.atomic.AtomicLong">
                <constructor-arg index="0" value="0"/>
            </bean>
        </entry>
    </map>
</constructor-arg>
</bean>

I just took your code and replaced com.company.EventType I didn't have by String ... Maybe you should look at com.company.EventType because it is the only difference with my test.

Upvotes: 2

Simeon
Simeon

Reputation: 7792

This isn't actually an answer, but I found a way that works.

<bean id="totalEventCountStore" class="java.util.concurrent.ConcurrentHashMap">
    <constructor-arg ref="eventCountInit"/>
</bean>

<util:map id="eventCountInit" map-class="java.util.HashMap" key-type="com.company.RouterDiagnosticEventType" value-type="java.util.concurrent.atomic.AtomicLong">
    <entry key="ROUTED_REQUEST">
        <bean class="java.util.concurrent.atomic.AtomicLong"/>
    </entry>
    <entry key="ROUTED_REQUEST_WITH_METADATA">
        <bean class="java.util.concurrent.atomic.AtomicLong"/>
    </entry>

... more entreies like these ...

</util:map>

Still, I don't really know why this way works while the other doesn't, so if someone finds out I'd be really grateful.

And even more puzzling, where does that [int] arg come from ?

Upvotes: 1

Related Questions