Services and the Blueprint Container
In the Blueprint programming model, you use a service element to define the registration of a service in the OSGi service registry. You use the ref attribute to reference the bean that provides the service object. You use the interface attribute to specify the interfaces under which the service is registered.
See the following partial Java™ class and Blueprint XML example code.
public class AccountImpl implements Account {
public AccountImpl() {
...
}
}
<service id="serviceOne" ref="account"
interface="org.apache.aries.simple.Account" />
<bean id="account" class="org.apache.aries.simple.AccountImpl" />
You can specify the bean that provides the service object by using an inline declaration in the service element, as shown in the following Blueprint XML example code.
<service id="serviceTwo" interface="org.apache.aries.simple.Account">
<bean class="org.apache.aries.simple.AccountImpl" />
</service>
You can use the auto-export attribute to set the interfaces under which a service is registered. The following Blueprint XML example code registers the service under all the interfaces of the bean.
<service id="serviceOne" ref="account" auto-export="interfaces" />
<bean id="account" class="org.apache.aries.simple.AccountImpl" />
The default value for the auto-export attribute is disabled. Other values are class-hierarchy and all-classes.
- Service properties
- You can register a service with a set of properties by using the service-properties element. The
service-properties element contains multiple entry elements that represent the individual
properties. You specify the property key by using a key attribute. You specify the property value as
a value attribute, or in inline declaration in the element. Service property values can be different
types, but must be only OSGi service property types, that is, one of the following types:
- primitive
- primitive wrapper class
- collection
- array of primitive types
The following Blueprint XML example code shows a service registration with two service properties. The active service property has type of java.lang.Boolean. The mode property is of the default type, String.
<service id="serviceFour" ref="account" autoExport="all-classes"> <service-properties> <entry key="active"> <value type="java.lang.Boolean">true</value> </entry> <entry key="mode" value="shared"/> </service-properties> </service>
- Service ranking
- You can use service ranking to control the choice of service when there are multiple matches. If
there are two services, the higher ranked service is returned before the lower ranked one. The
default ranking value is 0. The following Blueprint XML example code shows how to specify service
ranking by using the ranking
attribute.
<service id="serviceFive" ref="account" auto-export="all-classes" ranking="3" />