Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring <constructor-arg> element must specify a ref or value

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)

like image 841
smsnheck Avatar asked Jun 23 '13 11:06

smsnheck


Video Answer


1 Answers

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.

like image 55
Mikhail Avatar answered Sep 23 '22 01:09

Mikhail