Search code examples
osgideclarative-servicesservice-factory

Service Factory implementation using declarative services in OSGi


I am registering an OSGi service using OSGi declarative service and setting

servicefactory="true"

as follows.

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="test.Configuration">
  <implementation class="test.ConfigurationImpl"/>
  <service servicefactory="true">
     <provide interface="test.Configuration"/>
  </service>
</scr:component>

I am trying to use the service multiple times from another bundle. But, it returns me the same instance of the service.

What could possibly go wrong here ??


Solution

  • The servicefactory flag means that each consumer bundle gets a separate instance. So if you consume the service from 2 bundles then you will get 2 instances. You do not get multiple instances per consumer.

    If you want programmatic control from the consumer over the number of instances then you need to use the ComponentFactory approach.