我正在尝试使用Java Bean创建我认为是简单继承结构的创建(我之前从未使用过它们)。我得到的代码有一个基类定义为
public class CreateDialogFactory {
priavte static beanId;
protected CreateDialogFactory() { }
protected CreateDialogFactory(String beanId) { this.beanId = beanId; }
public static CreateScenarioDialog getInstace() {
return SpringHelper.getInstance(beanId, CreateScenarioDialogFactory.class);
}
}
继承这个类是两个定义的类:
@Component(ACreateScenarioDialogFactory.BEAN_ID)
public class ACreateScenarioDialogFactory extends CreateDialogFactory {
private static String BEAN_ID = "create-a-scenario";
protected ACreateScenarioDialogFactory() {
super(BEAN_ID);
}
}
和
@Component(BCreateScenarioDialogFactory.BEAN_ID)
public class BCreateScenarioDialogFactory extends CreateDialogFactory {
private static String BEAN_ID = "create-b-scenario";
protected BCreateScenarioDialogFactory() {
super(BEAN_ID);
}
}
我的xml规范非常简单 -
我的问题是,当我编译时,我得到No unique bean of type [a.b.CreateDialogFactory] is defined: expected single matching bean but found 2: [create-a-scenario, create-b-scenario]
即使我在xml规范中注释掉bean定义。
答案 0 :(得分:0)
Spring中的控制反转旨在与接口一起使用。在您的代码中,层次结构的基类在生成bean时被视为类型,因此SpringHelper.getInstance(beanId, CreateScenarioDialogFactory.class)
无法在ACreateScenarioDialogFactory
或BCreateScenarioDialogFactory
之间进行选择,因为它们是相同的类型。