“无法解析匹配构造函数”为子类传递constructor-arg时出错 [英] "Could not resolve matching constructor" error when passing in constructor-arg for child class

查看:1988
本文介绍了“无法解析匹配构造函数”为子类传递constructor-arg时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

  public abstract class ParentClass 
{
{
throw new RuntimeException(必须指定ID。
}

public ParentClass(String id)
{
this(id,DEFUALT_ARG_VALUE);
}

public ParentClass(String id,int anotherArg)
{
this.id = id;
// stuff with anotherArg
}

public abstract void doInstanceStuff();
}

public class ChildClass extends ParentClass
{
@Override
public void doInstanceStuff()
{
//。 ...
}
}



在我的应用程序上下文中,

 < bean id =myChildInstanceclass =com.foo.bar.ChildClassscope =singleton> 
< constructor-arg value =myId/>
< / bean>

问题是,当服务器启动时,我收到以下错误:



org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源中定义的名为'ivpluginHealthCheckTest'的bean [/ WEB-INF / spring / context.xml]:无法解析匹配的构造函数(提示:为简单参数指定索引/类型/名称参数以避免类型模糊)

看到错误,我尝试添加不同的属性,但没有运气。我最终得到这样的:

 < bean id =myChildInstanceclass =com.foo.bar.ChildClass scope =singleton> 
< constructor-arg value =myIdindex =0type =java.lang.Stringname =id/>
< / bean>

我仍然遇到同样的错误。



我试着添加相同的构造函数到我的子类,只需调用 super()与适当的参数为每一个,似乎修复它。但是,我不想在所有子实例中添加相同的构造函数,并且必须使用父类来维护它们。



有一些原因麻烦调用继承的构造函数来实例化类?

解决方案


调用一个继承的构造函数来实例化class?


构造函数不会被继承,它并没有什么意义。构造函数只是初始化该特定类中的状态。您不能期望 Parent 的构造函数初始化 Child 类的状态。这是 Child 类中的构造函数的作用。



所以,你不能做你想做的去做。这不是Spring的问题。这是基本的。


I have the following classes:

public abstract class ParentClass
{
    public ParentClass()
    {
        throw new RuntimeException("An ID must be specified.");
    }

    public ParentClass(String id)
    {
        this(id, DEFUALT_ARG_VALUE);
    }

    public ParentClass(String id, int anotherArg)
    {
        this.id = id;
        //stuff with anotherArg
    }

    public abstract void doInstanceStuff();
}

public class ChildClass extends ParentClass
{
    @Override
    public void doInstanceStuff()
    {
        //....
    }
}

In my application context I have this:

<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
    <constructor-arg value="myId" />
</bean>

The problem is that when the server starts up I get the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ivpluginHealthCheckTest' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

Seeing the error, I tried adding the different attributes, but with no luck. I ended up with something like this:

<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
    <constructor-arg value="myId" index="0" type="java.lang.String" name="id" />
</bean>

And I still get the same error.

I tried adding the same constructors to my child class, and just call super() with the appropriate arguments for each one, and that seems to fix it. However, I don't want to have to add the same constructors in all my child instances and have to maintain those with the parent class.

Is there some reason Spring has trouble calling an inherited constructor to instantiate the class? Is there something I can do to make this work?

解决方案

calling an inherited constructor to instantiate the class?

Constructors are never inherited, and it doesn't really make sense. A constructor just initializes the state in that particular class. You can't expect a constructor of Parent to initialize the state of Child class. That's the job of constructor in Child class only.

So, no you can't do what you are trying to do. And that's not the issue with Spring. That's pretty much fundamental.

这篇关于“无法解析匹配构造函数”为子类传递constructor-arg时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆