gpt4 book ai didi

java - Spring 元素必须指定一个 ref 或 value

转载 作者:IT老高 更新时间:2023-10-28 13:49:48 25 4
gpt4 key购买 nike

我在使用 Spring 和构造函数注入(inject)时遇到问题。我想动态创建具有名称 (String) 和特殊 id (long) 的对象。

但是当spring.xml文件加载的时候出现异常。

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?

我的 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>

那有什么问题呢?构造函数参数的索引为 1(而不是 0,正如异常所说)

最佳答案

在构造函数参数中,您可以使用原始类型 long 和值 0,或包装器类型 java.lang.Long 和一个空值。此外,为了控制一切,我会将第二个参数的值显式设置为 0。

关于java - Spring <constructor-arg> 元素必须指定一个 ref 或 value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com