組件A 負責載入初始化spring及資料庫 組件B負責對外API及RMI綁定,組件B引用組件A中的DAO對象,步驟如下
1 建立 組件A與B的介面與實現
組件A
@Remotable//涉及到存根引用所以要加此註解
publicinterface ServiceApi {
EmployeesImp getName();
}
publicclass ServiceApiBeanimplements ServiceApi {
@Resource(name="employeesImp")
private EmployeesDaoeapi;
public EmployeesImp getName() {
System.out.println(eapi.find("admin").getPassword());
returnnull;
}
}
組件B
@Remotable
publicinterface Hello {
void printName();
}
publicclass HelloBeanimplements Hello{
private ServiceApiapii;
public ServiceApi getApii() {
returnapii;
}
@Reference
publicvoid setApii(ServiceApi apii) {
this.apii = apii;
}
publicvoid printName() {
//System.out.println(apii.getName());
System.out.println(apii.getName()+"");;
}
}
2、配置構建描述設定檔
組件A
<?xmlversion="1.0"encoding="UTF-8"?>
<compositexmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:sca="http://www.osoa.org/xmlns/sca/1.0"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"
xmlns:c="http://gpwBusinessCenter"
targetNamespace="http://gpwBusinessCenter"
name="ServiceCenter">
<componentname="ServiceApiComponent"autowire="true">
<implementation.springlocation="com/sca/resource/ServiceApi.xml"/>
</component>
</composite>
組件B
<?xmlversion="1.0"encoding="UTF-8"?>
<compositexmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:sca="http://www.osoa.org/xmlns/sca/1.0"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"
xmlns:c="http://gpwBusinessCenter"
targetNamespace="http://gpwBusinessCenter"
name="helloCenter">
<sca:servicename="Hello"promote="HelloServiceComponent">
<interface.javainterface="com.sca.resource.Hello"/>
<tuscany:binding.rmiserviceName="HelloServiceComponentRMI"host="localhost"port="8999"></tuscany:binding.rmi>
</sca:service>
<componentname="HelloServiceComponent"autowire="true">
<implementation.springlocation="com/sca/resource/Hello.xml"/>
<referencename="apii"target="ServiceApiComponent"></reference>//這裡的引用與spring中的<property 引用的一樣
</component>