Apache Tuscany 運行時支援將多個 Spring ApplicationContext 合成為單一 SCA 組件實現,並提供 @Service、@Reference、@Property 註解在 Bean 類中顯式宣告 SCA 契約,超越 SCA Spring 規範 1.0。本文詳細說明組合上下文機制、註解用法與綁定配置。
在 <implementation.spring> 的 location 指向一個定義 ClassPathXmlApplicationContext Bean 的上下文檔(如 beanRefContext.xml),Tuscany 會將其構造參數列表中的多個上下文檔合併為單一組合上下文。
<component name="CalculatorServiceComponent">
<implementation.spring location="beanRefContext.xml"/>
</component>
<beans ...>
<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>
運行時行為:
SCAParentApplicationContext 注入引用與屬性ClassPathXmlApplicationContext 實例作為組件實現在 Bean 類直接宣告契約,替代 XML <sca:service> 等元素。
@Service(CalculatorService.class) — 發布服務,指定介面@Reference — 注入引用,支援欄位、Setter、建構子注入@Property — 注入 SCA 屬性@Service(CalculatorService.class)
public class CalculatorServiceImpl implements CalculatorService {
// Setter 注入
public AddService addService;
// 欄位注入
@Reference
public SubtractService subtractService;
// 欄位注入(名稱不同)
@Reference(name = "multiplyService")
public MultiplyService multiply;
// Setter 注入(名稱不同)
public DivideService divide;
@Reference(name = "divideService")
public void setDivideService(DivideService divide) { this.divide = divide; }
// 屬性注入
public String message;
@Property
public void setMessage(String message) { this.message = message; }
}
<component name="CalculatorComponent">
<implementation.spring location="META-INF/spring/calculator-context.xml"/>
<reference name="addService" target="AddComponent"/>
<reference name="subtractService" target="SubtractComponent"/>
<reference name="multiplyService" target="MultiplyComponent"/>
<reference name="divideService" target="DivideComponent"/>
<property name="message">HelloWorld</property>
</component>
綁定(Web Services、JMS、RMI、JSON-RPC 等)完全在 Composite 層級配置,Spring 上下文零感知,實現傳輸協議與業務邏輯徹底分離。