Application Platform 10.3 | Application Platform API | Injecting Service Dependencies into Other Services
 
Injecting Service Dependencies into Other Services
Use the following annotation to inject service dependencies into other services.
@ServiceReference
Use this annotation to inject a service from the runtime registry into another service being published (using the @Service annotation). This provides a form of dependency injection, in which the injected dependency is another POJO/bean already published in the runtime as an OSGi service.
You must specify a setter method to set the injected POJO reference in the same class that accompanies the field declaration. This is the class that contains the @ServiceReference annotation.
The following table describes the properties of @ServiceReference and specifies the default value for each property.
Property
Default Value
Description
id
""
String Required. A unique identifier for this service reference. The specified id must not be in conflict with any other implicit or explicit @Service annotation name attribute value.
interfaces
{}
String List Required if the filter property is not specified, otherwise it is optional. The interfaces that the service reference proxy should implement when it is wired in from the service registry. A service that implements these interfaces must be available in the registry. At least one interface or class name must be specified for this service reference.
filter
""
String Required if the interfaces property is not specified, otherwise it is optional. An OSGi filter expression that constrains the service registry lookup to only those services that match the given filter. The filter string is in the following format: (property-name = value). For example, (asynchronous-delivery=true) restricts the service lookup to those services that have a property with name asynchronous-delivery that is set to true.
timeout
5000 ms
Integer Optional. The amount of time (in milliseconds) to wait for a backing service to become available when an operation is invoked. If no matching service becomes available within this timeout period, an unchecked ServiceUnavailableException is thrown.
componentName
""
String Optional. A convenient shortcut for specifying a filter expression that matches the property named org.eclipse.gemini.blueprint.bean.name that is automatically advertised for beans, published with the @Service annotation.
dependsOn
""
String Optional. Specifies that the service reference should not be looked up in the service registry until the named dependent bean has been instantiated.
availability
Availability. OPTIONAL
ServiceReference.Availability Optional. Indicates the requirement for the availability of this service reference. By default, the reference is treated as an optional requirement. If set to MANDATORY, then the @Service registration will only succeed if the referenced service is already available.
Important:
Do not declare a mandatory reference to a service that is also exported by the same bundle. This can cause application context creation to fail through either deadlock or timeout.
The following example shows the GreeterImpl class published as an OSGi service that depends on the ResourceUtil class that is in turn published as another OSGi service.
@Service(name = “greeter-impl”, interfaces = 
{ “com.example.osgi.greet.api.IGreeter”,
                      “org.osgi.service.cm.ManagedService” }, properties = 
{ @Property(key = “service.pid”, values = “com.example.osgi.greet”) })
 
 
public class GreeterImpl implements IGreeter, ManagedService {
              public static final String KEY_HELLO = “hello”;
              private String key = KEY_HELLO;
 
             @ServiceReference(id = “resourceUtilRef”, interfaces =
{“com.example.osgi.greet.impl.ResourceUtil”})
                ResourceUtil resUtil;
 
                public void setResUtil(ResourceUtil resUtil) {
                             this.resUtil = resUtil;
                }
 
                ...
}
 
@Service
public class ResourceUtil {
             ...
}