具有此代码:
public class ClassA {
private InterfaceB interfaceB;
private int a
private int b;
public ClassA(int a, int b) {
this.a = a;
this.b = b;
}
}
我的应用程序中可能有几个ClassA
对象,它们是在运行时按需创建的。但是所有这些都应使用InterfaceB
类的相同具体实现(InterfaceB
的几种实现,但根据所使用的平台,在运行时仅使用一种实现)。在应用程序中(单个类)应该只有一个InterfaceB
对象。
当已知interfaceB
和ClassA
构造函数参数时,由于a
对象是在运行时创建的,因此我无法自动连接b
。>
我如何在这里使用Spring Framework来实现服务定位器模式?我的计划是在ClassA
中实例化服务定位符,并使用它来获取InterfaceB
对象。
具有此代码:公共类ClassA {private InterfaceB interfaceB;私人int私人int b;公共ClassA(int a,int b){this.a = a; this.b = b; }} ...
您可以创建其他类,这些类将创建您的ClassA
,并将保留对interfaceB
的引用。例如:
我认为您不需要服务定位器模式,在现代的Spring驱动的应用程序中,通常不再需要它。