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

半世苍凉 提交于 2019-12-23 07:48:28

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/17260258/spring-constructor-arg-element-must-specify-a-ref-or-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!