使用Spring Framework设计和开发SCA组件,第2部分2010-12-03 IBM Ramkumar Ramalingam使用Spring Framework设计和开发SCA组件,第2部分 - 使用Apache Tuscany的高级技巧简介本系列的 使用 Spring Framework 设计和开发 SCA 组件,第 1 部分:三剑客:Spring、SCA 和 Apache Tuscany 概述了综合使用 SCA 和 Spring 的益处。您了解了如何作为一种 SCA 服务公开 Spring bean 以及如何 在 Spring 应用程序内访问 SCA 服务和属性。本文将探讨 Apache Tuscany 运行时支持的一些高级特性。了解如何将多种 应用程序上下文综合起来用作 SCA 组件的一个实现。SCA 注释可用来在 Spring bean 类内显式声明 SCA 服务、引用和属性。下载 计算器示例的源代码。本文中的示例使用了 Apache Tuscany SCA Java™ 技术运行时 V1.5.1。要运行这个示例应用程序,需要下载 Apache Tuscany SCA Java 实现 的二进制发行版。为 SCA 组件使用多种应用程序上下文在 使用 Spring Framework 设 计和开发 SCA 组件,第 1 部分:三剑客:Spring、SCA 和 Apache Tuscany, 您了解了一个 Spring 应用程序可被定义为 SCA 复合集(即 SCDL)内的一个 SCA 组件,其格式如清单 1 所示。清单 1. 具有一个 Spring 组件的 SCA 复合集
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"
targetNamespace="http://calc"
xmlns:c="http://calc"
name="Calculator">
<component name="CalculatorServiceComponent">
<implementation.spring location="targetURI"/>
</component>
</composite>
<implementation.spring> 元素的 location 属性可将目标 URI 指定 为指向一个归档文件(JAR)、一个目录或直接指向一个 Spring 应用程序上下 文文件。在任何情况下,在使用 <implementation.spring> 组件的 location 属性时,Apache Tuscany 只允许一个应用程序上下文作为目标应用程 序上下文,用作 SCA 组件的实现。Apache Tuscany 允许使用多种应用程序上下文来实现 SCA 组件,方法是在 这个目标应用程序上下文(由此 SCA 复合集文件内的 <implementation.spring> 元素的 location 属性标识)中定义一个 ClassPathXmlApplicationContext(如 清单 3 所示)bean。清单 2 给出了一 个示例。清单 2. 具有一个 Spring 组件的 SCA 复合集
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"
targetNamespace="http://calc"
xmlns:c="http://calc"
name="Calculator">
<component name="CalculatorServiceComponent">
<implementation.spring location="beanRefContext.xml"/>
</component>
</composite>
清单 3. beanRefContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance"
xmlns:sca="http://www.springframework.org/schema/sca"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring- beans.xsd
http://www.springframework.org/schema/sca
http://www.osoa.org/xmlns/sca/1.0/spring- sca.xsd">
<bean class=” org.springframework.context.support.ClassPathXmlApplicationContext” >
<constructor-arg>
<list>
<value>context1.xml</value>
<value>context2.xml</value>
<value>context3.xml</value>
</list>
</constructor-arg>
</bean>
</beans>
这个 Apache Tuscany 运行时的意图是将具有一个 bean 定义(以 ClassPathXmlApplicationContext 作为 bean 类)的所有目标应用程序上下文 视为一个具有多种应用程序上下文的场景。这个 Tuscany 运行时之后将会为由 ClassPathXmlApplicationContext bean 的 constructor 参数标识的这列应用 程序上下文文件创建一个组合的应用程序上下文实例。在 清单 2 的例子中,被作为 CalculatorServiceComponent 的一个实现定 义的目标应用程序上下文是 beanRefContext.xml。在一个典型的场景中(在单 个应用程序上下文场景)中,Tuscany 运行时将会为 beanRefContext.xml 创建 一个应用程序上下文实例并将其用作 CalculatorServiceComponent 的一个实现 实例。在 清单 3 内,beanRefContext.xml 只定义了一个 bean 定义(以 ClassPathXmlApplicationContext 作为 bean 类)。这个场景被 Tuscany 运行 时视为是一个包含多种应用程序上下文的场景。由 ClassPathXmlApplicationContext bean 定义的 constructor 参数标识的这列 应用程序上下文文件(context1.xml、context2.xml 和 context3.xml)被综合 起来创建一个应用程序上下文实例,以用作 CalculatorServiceComponent 的一 个实现实例。图 1 展示了一个例子。对于一个包含多个上下文的场景,由 ClassPathXmlApplicationContext bean 定义的 constructor 参数标识的每个应用程序上下文文件都可具备其自己 的 SCA 服务、引用和属性(以显式或隐式的方式声明)。图 1. 具备 ClassPathXmlApplicationContext 的 SCA 运行时