Search code examples
osgiequinoxdeclarative-services

Is a declarative service a singleton by default?


The configuration:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" immediate="true" name="printing">
   <implementation class="printing.PrinterManager"/>
   <service>
      <provide interface="service.printing.IPrinterManager"/>
   </service>
</scr:component>

The class:

public class PrinterManager implements IPrinterManager {

    public void activate(BundleContext ctx) {
        System.err.println("hello");
    }

    public void deactivate(BundleContext ctx) {
        System.err.println("bye");
    }
}

When I inject an object of that class somewhere else in my code like this:

@Inject
IPrinterManager pm;

Do I always get the same instance of that class or not? How to make that instance a singleton with the help of osgi/equinox facilities if it's not a singleton already? (I know how to write a singleton the java/vanilla way, that's not my question)


Solution

  • Yes a DS component is a singleton by default. See Multiple Component Instances with OSGi Declarative Services