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

251 Views Asked by At

I am trying to use component factory in OSGi. My intention is to create instances of Class A, which is exposed as a factory, using Class B, which is a factory manager. Class A contains some services with @Reference annotations which work fine. The same approach is used for Class C, factory (as similar to Class A) with same service references with @Reference.

  1. Will it return the same service object to both the class A & Class 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)
  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?
  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. Is it possible/achievable in DS?
1

There are 1 best solutions below

2
On

ServiceFactory behavior is independent of 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. Different bundles will see different service objects (assuming the ServiceFactory implementation returns distinct objects).

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.

If you need distinct service objects, you should look into PrototypeServiceFactory added in Core R6 and supported in DS 1.3.