1
、
Spring
客户程序与实现模型
1.1
、目标
粗粒度的集成:与Spring的集成将是在
SCA 构件(composite)层的,在
SCA 构件层中,Spring application context提供了完整的构件,该构件通过
SCA 来暴露服务和使用引用。这意味着Spring application context定义了构件实现的内部结构。
1.2
、将
Spring Application Context
作为构件的实现
Spring Application context能用于实现某个Spring构件组件。概念上来说,可能如下所描述:
图1的上部阐述了由两个构件组成的简单的
SCA 域,这两个构件都是由Spring application context来实现的。
在该图中,有由独立的Spring Application Context定义的两个构件,每个构件都有一个声明的服务。Composite A由两个Spring bean组成,bean X是通过一个
SCA 服务暴露给
SCA 。Bean Y拥有一个对外部
SCA 服务的引用。该服务引用被连线于另一个Spring context,Composite B。该构件有单个声明性服务入口,该服务入口被连线于Bean Z。
使用Spring作为实现的组件不需要引入
SCA 元数据到Spring配置中就可以连线
SCA 的服务和引用。Spring context对
SCA 环境知晓得非常少。所有的策略强制性都发生于
SCA 运行时的实现中,并不会进入到Spring的空间中。
图2
图2展示了
SCA 运行时与Spring context交互的两点:服务和引用。任意的策略强制性都是由
SCA 运行时在最终消息到达目标Spring bean之前,调用进入Spring application context的时候来完成的。在来自applicaction context的向外部的调用时,由
SCA 运行时提供的引用提供了策略强制性。
寄宿于
SCA 运行时,并实现了构件的Spring application context创建一个parent application context。在该parent application context中,所有的
SCA 引用都作为bean来定义。注意,该bean使用
SCA 引用名作为bean的名字。这些bean在child context(也称为用户应用)中都自动可见。
如下的spring configuration为上述图1中表达的Spring application context A提供了模型。在本例中,有两个Spring bean,X和Y。名为”X”的bean是从
SCA 进入Spring context的入口点,Spring bean Y包含了一个由
SCA 提供的服务的引用。
<beans>
<bean id="X" class="org.xyz.someapp.SomeClass">
<property name="foo" ref="Y"/>
</bean>
<bean id="Y" class"org.xyz.someapp.SomeOtherClass">
<property name="bar" ref="SCAReference"/>
</bean>
</beans>
定义了两个bean。名为”X”的bean包含了一个名为”foo”的属性域,引用到上下文中的第二个bean,名字为”Y”。bean “Y”也有一个名为”bar”的属性域,引用到
SCA 服务的引用,名为”
SCA Reference”。
SCA SCDL 包含了带有相应绑定信息的Spring构件的服务和引用的定义。
<composite name="BazComposite">
<component name="SpringComponent">
<implementation.spring location=".."/>
<service name="X"/>
<reference name="SCAReference" .../> <!-- binding info specified -->
</component>
</composite>
这里唯一与Spring相关的就 是<implementation.spring>元素。该元素的location属性指定了一个archive文件或包含了Spring application context文件的目录的目标URI地址。用于创建application context的Spring application context配置文件的资源路径按如下方式被标识:
如果被location属性标识的资源是一个archive文件,那么将 读取archive文件的META-INF/MANIFEST.MF文件。如果location URI所标识的是一个目录,那么在该目录下必须存在MET-INF/MANIFEST.MF文件。如果manifest文件包含以下形式的头部信息:
Spring-Context ::=path(‘;’ path)*
这里的path是location URI的相对路径,那么在头部中指定的path集合就用来标识context configuration文件。如果没有MANIFEST.MF文件或在manifest文件中没有Spring-Context的头部信息,那么默认 将使用在META-INF/spring目录下所有*.xml文件来构建application context。
<implementation.spring> 使用的每 个<service>元素在其name属性中都应该包含被暴露为SCA服务的Spring bean的名字。所以,对于Spring,服务的name属性起来两个作用:它标识一个Spring bean,并且其命名组件的服务。SCDL也包含了名为”SCAReference”的<reference>元素。该引用名变成为一个可寻 址Spring application context的名称。所以,这种情况下,”
SCA Reference”才能在上面的Spring configuration中被bean “Y”引用。
SCA 运 行时负责设置引用并将以这些引用在spring context中的标识名作为bean暴露出来。这些工作一般都是通过创建一个parent context来完成的,而该parent context有相应被定义的bean,并且实现提供的上下文将变成该parent context的child context。所以引用—如”
SCA Reference” ,被bean “Y”用于其”bar”属性对上下文有效。
也可以在Spring configuration中显式声明与
SCA 相关bean来代理
SCA 引用。当继承在parent context中由
SCA 运行时创建的bean定义时,在child context中以相同的名字定义的bean将覆盖它。这么做的主要原因是这样可以让Spring容器来修饰bean(比如,使用Spring AOP)。
<sca:reference name="SCAReference" type="com.xyz.SomeType/>
Spring SCA 命名空间支持提供一共三个元素。它们是:
<sca:reference> 该元素定义了描述了某个
SCA service的Spring bean。注意:此处的
SCA service是在Spring application context的外部。
<sca:property> 该元素定义了描述了某个
SCA component的property。该property配置了Spring composite。
<sca:service> 该元素定义了Spring composite暴露为service的bean。它的功能就是为Spring composite提供组件类型信息。特别地,SCA运行时负责创建相应的绑定(binding),并应用必须的策略到那些基于SCDL配置的服务上。如 果<sca:service>入口没有在parent SCDL中配置,那么SCA运行时必须抛出configuration error。如果在Spring application context中没有<sca:service>元素指定,那么任意的bean都可以暴露为service。
以下案例展示了一个application context。该应用上下文暴露出一个服务,
SCA Service,并显式地为
SCA reference定义了一个bean,
SCA Reference。bean Y 的”goo”属性配置为名为”sca-property-name”的
SCA Property。
<beans>
<!-- this definition is not required, and the bean SCA Reference could also
have been inherited from the parent context -->
<sca:reference name=”SCAReference” type="com.xyz.SomeType"/>
<bean name="X">
<property name="foo" ref="Y"/>
</bean>
<bean name="Y">
<property name="bar" ref="SCAReference"/>
<property name="goo" ref="sca-property-name"/>
</bean>
<!-- expose an SCA property named “sca-property-name” -->
<sca:property name="sca-property-name" type="java.lang.String"/>
<!-- expose the bean "X" as an sca service named "SCAService" -->
<sca:service name="SCAService" type="org.xyz.someapp.SomeInterface" target="X"/>
</beans>
2.
附录
2.1. Spring SCA Namespace schema
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
attributeFormDefault
=
"unqualified"
elementFormDefault
=
"qualified"
<
xsd:element
name
=
"composite"
>
<
xsd:complexType
>
<
xsd:attribute
name
=
"component"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"sca-adapter-class"
use
=
"optional"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
</
xsd:complexType
>
</
xsd:element
>
<
xsd:element
name
=
"reference"
>
<
xsd:complexType
>
<
xsd:attribute
name
=
"name"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"type"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"default"
use
=
"optional"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
</
xsd:complexType
>
</
xsd:element
>
<
xsd:element
name
=
"property"
>
<
xsd:complexType
>
<
xsd:attribute
name
=
"id"
use
=
"optional"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"name"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"type"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
</
xsd:complexType
>
</
xsd:element
>
<
xsd:element
name
=
"service"
>
<
xsd:complexType
>
<
xsd:attribute
name
=
"name"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"type"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
<
xsd:attribute
name
=
"target"
use
=
"required"
>
<
xsd:simpleType
>
<
xsd:restriction
base
=
"xsd:string"
/>
</
xsd:simpleType
>
</
xsd:attribute
>
</
xsd:complexType
>
</
xsd:element
>
</
xsd:schema
>
2.2.
参考资料
[1] Spring Framework

1231

被折叠的 条评论
为什么被折叠?



