Question

I'm having a problem with Spring and constructor injection. I want to create dynamically objects with a name (String) and special id (long).

But when the spring.xml file is loaded an exception occurs.

Exception in thread "main" java.lang.ExceptionInInitializerError

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

My spring.xml:

    <bean id="someBean" class="someClass" >
        <constructor-arg index="0" type="java.lang.String" value=""/>       
        <constructor-arg index="1" type="long" value=""/>
    </bean>
</beans>

So what's wrong there? The constructor-arg has index 1 (and not 0, as the exception says)

Was it helpful?

Solution

In the constructor arguments you can use either a primitive type long and value 0, or a wrapper type java.lang.Long and an empty value. Also, to keep things under control, I would set a value of the second argument explicitly to 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top