Application Platform 10.3 | Application Platform API | Publishing POJOs as OSGi Services
 
Publishing POJOs as OSGi Services
Use the following annotations to publish POJOs as OSGi services.
@Service
Use this annotation to mark a POJO class to be exposed as an OSGi service. Specify @Service on the class type.
For example:
@Service(name = "my-service", init = "start", destroy = "stop", ranking = "10", 
interfaces = { "com.example.MyInterface" }, properties = { @Property(key = 
"key1", values = {1, 2, 3}, valueType = "java.lang.Integer") }) 
public class MyService implements MyInterface { 

 
interface MyInterface { 
}
The following table describes the properties of @Service and specifies the default value for each property.
Property
Default Value
Description
name
Simple name of the annotated class
String Optional. The name of the bean backing this service. If you do not specify a value, this property defaults to the simple name of the bean class.
value
Simple name of the annotated class
String Optional. An alternative way to specify the name of the service bean. This property is useful when you do not specify any other attributes.
ranking
0
Integer Optional. The ranking value to be published as the service.ranking property for this service to distinguish.
init
""
String Optional. The method to invoke when the bean that backs the service is initialized.
destroy
""
String Optional. The method to invoke when the bean that backs the service is destroyed.
interfaces
The fully qualified name (FQN) of the annotated class
String List Optional. The list of interfaces, under which the service will be published. If you do not specify a value for this property, the service will only be published under the name of the implementation class.
dependsOn
""
String Optional. Used to express a dependency on another component that must be fully initialized before this service can be initialized and exported.
properties
{}
String Optional. The list of service properties to be published with the service.
@Property
Use this annotation to declare the properties for the service. You can add more than one value for the key. Optionally, you can also specify the type of the key and the type of the values.
The following table describes the properties of @Property and specifies the default value for each property.
Property
Default Value
Description
key
""
String Required. The name or key of the property.
values
{}
Sting List Required. The values to be associated with the property name.
valueType
java.lang.Sring
String Optional. The type of the values of this property.
The following example shows the GreeterImpl POJO class registered as an OSGi service under the name "greeter-impl", as well as two interfaces and one service property.
public interface IGreeter { 
       public String greetMe(String name); 

 
@Service( 
                          name="greeter-impl", 
                          interfaces = {"com.example.osgi.greet.api.IGreeter", 
"org.osgi.service.cm.ManagedService"}, 
                          properties = {@Property(key="service.pid", val- 
ues="com.example.osgi.greet")} 
                                ) 
public class GreeterImpl implements IGreeter, ManagedService { 
            @Override 
       public String greetMe(String name) { 
              return "Hello, " + name;