SpringDM笔记15-通过声明特定的属性注册和引用服务

1. Configuration for registering services:The tag:service

    (1)BEAN REFERENCE

    Spring DM also supports the use of an anonymous bean(inner bean) within the service tag, as

    shown  in the following:

    <osgi:service id="testService" interface="com.manning.sdmia.springdm.service.TestService">
        <bean class="com.manning.sdmia.springdm.service.impl.TestServiceImpl"/>
    </osgi:service>

    其中,bean:TestServiceImpl无Id,不能在Spring配置文件中其他地方使用。

    (2) SERVICE INTERFACE SUPPORT

    Because OSGi allows you to register services under several identifiers, Spring DM also provides

    an inner interfaces tag that accepts a list of values, as shown in the following:

    <osgi:service id="testService" ref="testServiceBean">
           <osgi:interfaces>
              <value>
                     com.manning.sdmia.springdm.service.TestService
             </value>
             <value>(...)</value>
           </osgi:interfaces>
    </osgi:service>

    In addition to this explicit configuration of interfaces, Spring DM provides a mechanism to detect

    values used for the interface attribute and interfaces tag. This mechanism is configured through

    the auto-export attribute and provides alternative strategies to autodetect interfaces when the

    interface attribute and interfaces tag are not specified, as described follow:

    ■  disabled:Default strategy where you need to explicitly specify values of the interface

        attribute and interfaces tag;

    ■  interfaces:Strategy using all the interfaces implemented by the service;

    ■  class-hierarchy:Strategy using the class and all parent classes of the service;

    ■  all-classes:Corresponds to both the interfaces and class-hierarchy strategies。

    示例:

    <osgi:service id="testService" ref="testServiceBean" auto-export="interfaces"/>

    (3) SERVICE PROPERTY SUPPORT,使用service-properties标签:

    <osgi:service id="testService" ref="testServiceBean"

             interface="com.manning.sdmia.springdm.service.TestService">
             <osgi:service-properties>

                  <entry key="creationDate" value="2009-12-12"/>
                  (...)
             </osgi:service-properties>

    </osgi:service>

    (4)MANAGING THE CLASSLOADING CONTEXT

    If TCCL specified in the service registration configuration , Spring DM makes it possible during the

    service call to see all the classes of the bundle that registers the service,When exporting a bean

    as an OSGi service, Spring DM allows you to specify which classes are reachable from this 

    service based on the TCCL managed by Spring DM. At this level, either no classes, or classes

    present in the bundle that registers service, will be reachable.This behavior can be configured

    using the context-class-loader attribute of the service tag. For example:

    <osgi:service id="testService" ref="testServiceBean" 

              interface="com.manning.sdmia.springdm.service.TestService"
              context-class-loader="service-provider"/>

    Context classloading strategies for the context-class-loader attribute:

    ■ unmanaged:This is the default strategy, where no context classloader management is done.

    ■ service-provider:This strategy allows the service implementation, during the service call, to
       see all the classes of the bundle that registered the service.

    (5)SERVICE-RANKING SUPPORT

    Spring DM allows you to specify a rank for a service through the service tag’s ranking attribute.

    This attribute is optional and its default value is 0. The following snippet shows how to use this

    attribute:

    <osgi:service id="testService" ref="testServiceBean"
             interface="com.manning.sdmia.springdm.service.TestService" ranking="5" />

    OSGi’s ranking feature is used when you’re trying to obtain a service reference and there’s more

    than one service match. In this situation, the service with the highest ranking will match. The

    ranking of services is also used by Spring DM as a tiebreaker in its shutdown algorithm.

    (6)BUNDLE SCOPE

    SpringDM提供了一个Bean Scope:bundle,而OSGi 规范也规定了一个借口:ServiceFactory,用于针对特定

    的服务请求创建每个Bundle的服务实例;SpringDM实现这种机制没有使用OSGi规范中ServiceFactory接口,

    而是使用bundle Scpoe来实现.

    The configuration of the service factory feature using bundle scope must be done on the bean (in

    the bean tag) in order to export the bean as a service. It is not done on the service exporter (the

    service tag of the osgi namespace). The following snippet shows this:

    <osgi:service id="testService" ref="testServiceBean"
             interface="com.manning.sdmia.springdm.service.TestService"/>
    <bean id=" testServiceBean" scope="bundle"
             class="com.manning.sdmia.springdm.service.impl.TestServiceImpl">
             (...)
    </bean>

2. Configuration for referencing services(using a single service.)

    (1)SERVICE INTERFACE SUPPORT

    <osgi:reference id="testService">
          <osgi:interfaces>
                <value>
                      com.manning.sdmia.springdm.service.TestService
                </value>
                <value>(...)</value>
          </osgi:interfaces>
    </osgi:reference>

    (2)SERVICE AVAILABILITY IN REFERENCING

    The availability attribute of the reference tag allows you to configure whether or not service is

    optional, and it accepts the values listed follow:

    Values for the availability attribute:

    ■ optional:This value specifies that the service is optional and doesn’t need to be available all

       the time.

    ■ mandatory:This is the default value, which specifies that the service is mandatory for the

      component to function correctly. The service must always be present for the component’s

      execution.

    注意:Before Spring DM version 2, the availability attribute was named cardinality . In that

    context, the value 0..1 was the equivalent of the optional value, and 1.. was the equivalent

    of mandatory.

    Setting the value for the availability attribute specifies how Spring DM should behave during

    service matching and, more particularly, when the service can’t be immediately resolved.

    Different behaviors of Spring DM depending on service availability

    ■ On Spring container startup

       Availability:mandatory

       Description:The container waits until the service is available before completing its startup.

       If the service isn’t available after the specified timeout, the container throws a

       ServiceUnavailableException exception and the container isn’t started.

       Availability:optional

       Description:The container starts even if the referenced service isn’t available.

    ■ Spring container started

       Availability:mandatory

       Description:On a service call, if the service isn’t available, the container waits until the service

       becomes available to execute the call. If the service isn’t available after the specified timeout,

       the container throws a ServiceUnavailableException exception.

       Availability:mandatory

       Description:It behaves in the same way as on Spring container startup.

    示例:

    <osgi:reference id="testService" interface="com.manning.sdmia.springdm.service.TestService"
             availability="optional" />

    <osgi:reference id="testService" interface="com.manning.sdmia.springdm.service.TestService"
             timeout="1000" depends-on="anotherBean"/>

    (3)CLASSLOADER MANAGEMENT

    As with service exporting, Spring DM provides support to automatically manage the TCCL when

    calling a referenced service. Spring DM allows a service call invocation to see all the classes of

    the calling bundle, or of the bundle that registered the service, through the setting of the TCCL.

    When referencing an OSGi service, Spring DM allows you to specify which classes are reachable

    from this service by setting the context classloader. At this level, either no classes, or classes

    present in the bundle that either registers or calls the service, will be reachable.

    The class visibility can be configured using the context-class-loader attribute of the service tag.

    <osgi:reference id="testService" interface="com.manning.sdmia.springdm.service.TestService"
             context-class-loader="service-provider" />

    Context classloading strategies for the context-class-loader attribute :

    ■ Strategy:client

    Description:This default strategy allows the implementation to see all the classes of the calling

    component during the service call.

    ■ Strategy: service-provider

    Description:This strategy allows the service implementation to see all the classes of the bundle

    that registered the service during its call.

    ■ Strategy:unmanaged

    Description:This strategy performs no context classloader management.

    注意:Classloader management for services can be configured both when registering and 

    referencing services. Spring DM will always override the reference setting if the service setting

    is configured.

    (4)SERVICE SELECTION

    Spring DM allows you to specify fine-grained selection of services at the level of the reference

    element. Two attributes are available:
    ■ filter: can be used as an expression to select a subset of services to be matched.
    ■ bean-name: allows you to select the service with the bean-name property set. For this to work,

    the service must have been registered using Spring DM or a tool that also uses the bean-name

    property.
    The following snippet shows how to select the service corresponding to the TestService interface

    and having a creationDate attribute with the value 2009-12-12:
    <osgi:reference id="testService" interface="com.manning.sdmia.springdm.service.TestService"
    filter="(creationDate='2009-12-12')"/>.

猜你喜欢

转载自springsfeng.iteye.com/blog/1152123