【发布时间】:2014-02-05 16:51:58
【问题描述】:
我有以下课程:
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)
看到错误,我尝试添加不同的属性,但没有成功。我最终得到了这样的结果:
<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 在调用继承的构造函数来实例化类时遇到问题是否有某些原因?我可以做些什么来完成这项工作吗?
【问题讨论】:
标签: java spring inheritance constructor