组件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>
</composite>
3配置SPRING实现
组件A
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sca="http://www.springframework.org/schema/sca"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/scahttp://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd">
<context:component-scanbase-package="com.sys"/>
<!-- 使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
需要在tomcat作一些特殊配置.具体参考手册
注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
-->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/register?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="initialPoolSize" value="1"/>
<property name="minPoolSize" value="1"/>
<property name="maxPoolSize" value="300"/>
<property name="maxIdleTime" value="60"/>
<property name="acquireIncrement" value="5"/>
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
-->
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">
<propertyname="driverClass"value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<propertyname="jdbcUrl"value="jdbc:sqlserver://172.16.171.81:49481;databaseName=gpw"/>
<propertyname="user"value="gpw"/>
<propertyname="password"value="useeasy"/>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<propertyname="initialPoolSize"value="1"/>
<!-- 连接池中保留的最小连接数。 -->
<propertyname="minPoolSize"value="1"/>
<!-- 连接池中保留的最大连接数。Default: 15 -->
<propertyname="maxPoolSize"value="300"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<propertyname="maxIdleTime"value="60"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<propertyname="acquireIncrement"value="5"/>
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
<propertyname="idleConnectionTestPeriod"value="60"/>
</bean>
<beanid="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="persistenceXmlLocation"value="classpath:jpa/persistence.xml"/>
<propertyname="loadTimeWeaver">
<beanclass="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<beanid="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">
<propertyname="entityManagerFactory"ref="entityManagerFactory"/>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
<sca:servicename="ServiceApi"type="com.sca.resource.ServiceApi"target="ServiceApiImp"></sca:service>
<beanid="ServiceApiImp"class="com.sca.resource.ServiceApiBean"> //绑定SCA与bean的关系
</bean>
</beans>
组件B
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sca="http://www.springframework.org/schema/sca"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/scahttp://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd">
<sca:servicename="Hello"type="com.sca.resource.Hello"target="HelloImp"></sca:service>
<beanid="HelloImp"class="com.sca.resource.HelloBean">
<propertyname="apii"ref="apii"></property>//这里的组件实现spring 引用到A组件所以,SCA中还是按原的方式进行装配及引用,spring中注入引用名,第一个name为spring管理的bean里的变量,第二个为sca配置文件中的<reference 的name值
</bean>
</beans>
需要注意的是:1各组件之间的引用可以通过SCA域来装配,这个装配
如
<?xmlversion="1.0"encoding="UTF-8"?>
<compositexmlns="http://www.osoa.org/xmlns/sca/1.0"xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0"xmlns:c="http://gpwBusinessCenter"targetNamespace="http://tuscany.apache.org/cloud"name="HelloNode">
<componentname="HelloNode">
<t:implementation.nodeuri="scacontribution"composite="c:helloCenter"/>
<servicename="Node">
<binding.scauri="http://localhost:8080"></binding.sca>
</service>
</component>
</composite>
2如何通过SPRING来引用
SCA的引用方式<reference name="变量" target="组件"/〉SPRING中的bean中也要用
<property name="变量" ref="变量""> 前一个变量为SPRING中的,引用SCA中的<reference name变量,意思就是告诉spring从哪找sca组件
3组件引用组件并调用引用组件的方法且返回接口对象是会报错误,在组件引用组件并调用引用组件的方法的的时候返回对象要为class不能为接口否则会报com.sys.dao.EmployeesDao is an interface, and JAXB can't handle interfaces.