首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-08-15 
SpringDM笔记15-通过声明特定的属性注册和引用服务1. Configuration for registering services:The tag:se

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 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"
???????????? 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')"/>.

热点排行