简体   繁体   English

通过在OSGI中不使用servicefactory的情况下使用Componentfactory创建唯一的服务实例

[英]creating a unique service instances using Componentfactory by without using servicefactory in OSGI

I am trying to use component factory in OSGi. 我正在尝试在OSGi中使用组件工厂。 My intention is to create instances of Class A, which is exposed as a factory, using Class B, which is a factory manager. 我的意图是使用工厂经理B类创建A类实例,该实例作为工厂公开。 Class A contains some services with @Reference annotations which work fine. 类A包含一些带有@Reference批注的服务,它们可以正常工作。 The same approach is used for Class C, factory (as similar to Class A) with same service references with @Reference. 对于具有@Reference的相同服务引用的C类工厂(与A类相似)使用相同的方法。

  1. Will it return the same service object to both the class A & Class C? 它将向A类和C类返回相同的服务对象吗?
  2. If yes(for question 1), whether service factory will work for the services in class A & Class C?(services with attribute servicefactory inside component factory (class A&C) 如果是(针对问题1),服务工厂是否将为A类和C类服务工作(组件工厂内部具有属性servicefactory的服务(A&C类))
  3. If not (for question 1), Instead of factories is there any approach to get the unique service object inside Class A & Class C while using @Reference? 如果不是(针对问题1),那么使用@Reference时,是否有任何方法可以在类A和类C中获取唯一的服务对象,而不是工厂?
  4. I can achieve the same using new operator for those services in the class A & class C respectively but I am expecting some kind of DI approach. 我可以使用new运算符分别为A类和C类中的那些服务实现相同的功能,但是我期望使用某种DI方法。 Is it possible/achievable in DS? 在DS中是否可能/可以实现?

ServiceFactory behavior is independent of DS. ServiceFactory的行为独立于DS。 Assuming you are not using the new PrototypeServiceFactory in R6, a bundle making multiple gets for a ServiceFactory service will see the same service object. 假设您未在R6中使用新的PrototypeServiceFactory,则为ServiceFactory服务进行多次获取的捆绑包将看到同一服务对象。 Different bundles will see different service objects (assuming the ServiceFactory implementation returns distinct objects). 不同的捆绑包将看到不同的服务对象(假设ServiceFactory实现返回不同的对象)。

So if A and C are in the same bundle, they will see the same service object for referenced services independent of whether the referenced service is a ServiceFactory. 因此,如果A和C在同一个包中,则对于引用的服务,它们将看到相同的服务对象,而与引用的服务是否为ServiceFactory无关。

If you need distinct service objects, you should look into PrototypeServiceFactory added in Core R6 and supported in DS 1.3. 如果需要不同的服务对象,则应查看Core R6中添加的DS 1.3中支持的PrototypeServiceFactory。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM