SCA+SPRING组件之间的引用细节

本文介绍了一种将Spring框架与SCA(Service Component Architecture)集成的方法,详细展示了组件A和组件B之间的交互过程,包括定义接口、实现类、配置文件等方面的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组件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>

 

</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&amp;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. 

目录 ................................................................................................................................................................................... 5  1.  通用注解、API、客户程序和实现模型 ................................................................................................................. 7  1.1. 简介 ........................................................................................................................................................................... 7  1.2. 实现的元数据 ........................................................................................................................................................... 7  1.2.1. 服务元数据 ............................................................................................................................................................ 8  1.2.2.@Reference ........................................................................................................................................................... 8  1.2.3. @Property ............................................................................................................................................................. 9  1.2.4. 实现作用域:@Scope、@Init、@Destroy ....................................................................................................... 9  1.3 接口元数据 .............................................................................................................................................................. 10  1.3.1. @Remotable ....................................................................................................................................................... 10  1.3.2. @Conversational ................................................................................................................................................ 11  1.4. 客户 API .................................................................................................................................................................. 11  1.4.1. SCA构件访问服务 .............................................................................................................................................. 11  1.4.2. 非 SCA构件的实现访问服务 ............................................................................................................................ 11  1.5. 错误处理 ................................................................................................................................................................. 12  1.6. 异步与会话编程 ..................................................................................................................................................... 12  1.6.1. @OneWay ........................................................................................................................................................... 12 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值