Composing advanced features by using OSGi Declarative Services

Simple features can be controlled by using bundle activator classes and direct implementation of interfaces such as ManagedService and ServiceTracker. As relationships between bundles become more complex, it can be better to use facilities such as OSGi Declarative Services (DS) to decompose a feature into individual services. DS (sometimes known as the Service Component Runtime, or SCR) provides lifecycle and injection management of OSGi services.

About this task

Organizing your feature logic as a set of declarative services has a number of advantages:
  • Activation of the service (which includes loading the Java™ classes that provide the service) can be deferred until the service is used; allowing the server to start quickly and to keep resource use to a minimum.
  • A reference to the service is placed into the service registry, even when the service has not been activated, so that dependencies on the service can be resolved.
  • Dependencies on other services can be injected at runtime, and activation of the various services will be ordered based on such dependencies.
  • A service can be deactivated and reactivated when its service properties change, if required.

Detailed information about use of OSGi Declarative Services is available from a number of online resources, including the OSGi Community Wiki.

This task provides simple descriptions of how to declare your services to DS, how to obtain references to other services, and how to manage configuration properties for each service.