osgi: Using ServiceFactories?

后端 未结 3 1385
暖寄归人
暖寄归人 2020-12-09 22:05

I\'m currently trying to get a simple bundle containing a Service Factory running.

This is my factory class:

public class SvcFactory implements Servi         


        
3条回答
  •  萌比男神i
    2020-12-09 22:46

    It is actually rather simple ... DS creates an instance for each bundle, so with DS you do not implement Service Factory, DS does all the hard work. For example:

    @Service(serviceFactory=true) 
    public class MyServiceFactory implements XyzService {
    
       ...
       @Activate
       void activate(ComponentContext ctx) {
          System.out.println("Using bundle: " + ctx.getUsingBundle());
       }
    }
    

    Every time another bundle gets this XyzService, DS will create a new instance. You can use the ComponentContext (optionally passed in the activate method) to get the bundle that is using you.

提交回复
热议问题