Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameter

问题

在练习spring构造器注入方式的小程序的时候报错:
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘poeticDuke’ defined in class path resource [com/springinaction/springidol/spring-idol.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

原因和解决方法

报错信息大体是说创建poeticDuke这个bean的时候出错,出错的原因是与构造器不匹配,给出的线索是为bean的构造器参数指定正确的索引、类或者名字,防止引起混淆。

构造器方法如下:

    public PoeticJuggler(int beanBags, Poem poem) {
        super(beanBags);
        this.poem = poem;
    }

XML配置文件如下:

    <bean id="sonnet29" class="com.springinaction.springidol.Sonnet29" />

    <bean id="poeticDuke" class="com.springinaction.springidol.PoeticJuggler">
        <constructor-arg value="15" />
        <constructor-arg ref="sonnet29" />
    </bean>

报错信息说参数不匹配,猜测应该是指constructor-arg元素内的参数类型不匹配,这里”15”和”sonnet29”应当分别对应于int原始数据类型和Poem类,15肯定是int类型这没问题,那么再看sonnet29,它是类Sonnet29的bean,然后我去看类Sonnet29是怎么写的,才发现忘记去实现Poem接口了,然后加上接口,程序正常。

public class Sonnet29 implements Poem {/*Here goes body.*/}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值