我正在使用组件工厂来创建多个唯一实例,而这些实例又使用服务工厂来创建多个实例。我的结构看起来像 TInterface - 接口/服务 TInterfaceInline - 它实现了 TInterface 还包括另一个服务的@Reference。
Tinterfaceimpl1 - 扩展了 TInterfaceInline,它是一个具有过滤器属性的服务工厂。TConsumer - 通过在@Reference 中获取 TInterfaceInline 来使用它的组件工厂。
消费者
@Component(name = "TConsumer", factory = "TConsumer")
@Service
public class TConsumer implements IConsumer {
// @Reference(bind = "bindInterface1", unbind = "unbindInterface1", target =
// "(className=Interface)")
// private Tinterface interface1;
@Reference(referenceInterface = ParentProfile.class, bind = "bindInterface11", unbind = "unbindInterface11", target = "(className=interface1)", policy = ReferencePolicy.STATIC, cardinality = ReferenceCardinality.MANDATORY_UNARY)
private ParentProfile interface11;
@Activate
public void activate(BundleContext aBundleContext) {
System.out.println("Object = " + interface11);
}
protected void bindInterface11(ParentProfile interface11) {
this.interface11 = interface11;
System.out.println("ref object11 created");
}
protected void unbindInterface11(ParentProfile interface11) {
interface11 = null;
}
TinterfaceImpl
@Component(name = "TInterfaceImol")
@Service(serviceFactory = true,value=ParentProfile.class)
@Properties(value = { @Property(name = "className", value = "interface1") })
public class Tinterfaceimpl1 extends TinterfaceInline {
public Tinterfaceimpl1() {
System.out.println("Generalprofile class : " + this);
}
}
Tinterface内联
@Component(name = "TinterfaceInline ")
@Service
public class TinterfaceInline implements java.io.Serializable, ParentProfile {
// @Reference(bind = "bindtestin", unbind = "unbindtestin", cardinality =
// ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)
public Testint testin;
protected synchronized void bindtestin(Testint inter) {
this.testin = inter;
System.out.println("Profile Class : binded parameterresolver " + testin);
}
protected synchronized void unbindtestin(Testint inter) {
this.testin = inter;
}
}
父类
@Component(name = "PResolver")
@Service
public class ProfileClass implements Testint {
public ProfileImpl() {
System.out.println("Parameter class impl : which extends ");
}
}
除此之外,剩下的就是标记接口。如果我尝试在 karaf 中安装,则 Tinterfaceimpl1 的状态为 REGISTERED。即)我正在使用服务工厂并从组件工厂访问服务工厂。Servicefactory 类扩展了一个实现接口的类。Tinterfaceimpl1 注册的原因是什么。