Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameter 标签: 构造器注入Spring

问题:要么是因为构造方法改变了,要么就是构造方法入参实例化失败(比如没有实现)

问题

在练习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.*/}
 
原文地址:https://www.cnblogs.com/exmyth/p/5995725.html