2

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 ??

Ravish
  • 2,428
  • 2
  • 18
  • 24

1 Answers1

5

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.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • I want to concurrently use the service from the same consumer, but on a new instance of service. Is that possible by ComponentFactory approach ?? – Ravish Jun 21 '12 at 13:53
  • 2
    You can use the ComponentFactory as Neil says but it is also a good idea to just register a FooFactory in that case, that allows you to stay in a type safe world. – Peter Kriens Jun 21 '12 at 15:47