我有以下类别:“无法解析匹配构造函数”传递时错误构造带参数的子类
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()
{
//....
}
}
在我的应用程序方面,我有这样的:
<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
<constructor-arg value="myId" />
</bean>
问题是当服务器启动时,我得到以下错误:
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)
看到错误,我尝试加载g不同的属性,但没有运气。我结束了这样的事情:
<bean id="myChildInstance" class="com.foo.bar.ChildClass " scope="singleton">
<constructor-arg value="myId" index="0" type="java.lang.String" name="id" />
</bean>
而且我仍然得到同样的错误。
我试着将相同的构造函数添加到我的子类中,并用每个适当的参数调用super()
,并且似乎解决了这个问题。但是,我不想在所有的子实例中添加相同的构造函数,并且必须使用父类来维护这些构造函数。
是否有某些原因Spring有麻烦调用继承构造函数来实例化类?我能做些什么来完成这项工作?
它不像你发出声音那么明显。我们假设所有的方法都是通过从类继承而来的,而构造函数定义在技术上是一种方法,所以我可以看到怎么会认为有可能继承构造函数方法。 –
@MohammadS。嗯。是啊,你说得对。删除了我的答案的那部分。 –
你在说什么是有道理的。有一次,我只在父类中留下了默认的构造函数,而在子类中没有任何内容,并且它抛出了异常。我想这是因为默认构造函数暗含超级调用(除非我也误解了这一点)。这只是让我觉得其他的也被继承了。 – dnc253