Java 如果实例是在if-else语句中创建的,如何配置Springbean?

Java 如果实例是在if-else语句中创建的,如何配置Springbean?,java,spring,Java,Spring,我的班级结构如下所示: class Base {} class ChildA extends Base {} class ChildB extends Base {} 我在我的申请代码中得到: Base theObj = null; if( str.equalsIgnoreCase("object_A") == true ) { theObj = new ChildA(); } else { theObj = new ChildB(); } 我可以知道如何在Spring中配置

我的班级结构如下所示:

class Base {}

class ChildA extends Base {}

class ChildB extends Base {}
我在我的申请代码中得到:

Base theObj = null;
if( str.equalsIgnoreCase("object_A") == true ) {
   theObj = new ChildA();
} else {
   theObj = new ChildB();
}

我可以知道如何在Spring中配置bean吗?

一个选项是在SpringXML上下文中为两个不同的bean(childA,childB)创建引用。然后在主bean的创建中,使用args指定构造函数。您可以使用前面提到的类似代码实现构造函数。

如果您正在创建一个带有“new”的对象JacobM:我想他是想说他如何在Spring中实现它。看起来这与你的情况有关:我想说的是使用AbstractFactoryBean,因为它包含一个逻辑选择,在运行时创建哪个子bean。