In an Eclipse or OSGi runtime, each individual bundle is its own separate module, which has explicit dependencies on library code via Import-Package
, Require-Bundle
, or Require-Capability
. These express static relationships and provide a way of configuring the bundle's classpath.
However, this presents a problem. If services are independent, how can they use contributions provided by other bundles? In Eclipse's case, the extension registry covered in Chapter 2, Creating Custom Extension Points, provides a means for code to look up providers. In a standalone OSGi environment, OSGi services provide a similar mechanism.
A service is an instance of a class that implements a service interface. When a service is created, it is registered with the services framework under one (or more) interfaces, along with a set of properties. Consumers can then get the service by asking the framework for implementers of that specific interface.