Tuscany简单应用
简单的例子:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0"
xmlns:c="http://sample"
targetNamespace="http://sample"
name="sample">
<!-- 对外接口 访问地址:http://localhost:8080/echoComponent?wsdl-->
<service name="echo" promote="echoComponent" >
<interface.java interface="com.simple.echo.Echo"></interface.java>
<binding.ws uri="http://localhost:8080/echoComponent"></binding.ws>
</service>
<!--组件声明 -->
<component name="echoComponent">
<implementation.java class="com.simple.echo.EchoImp"></implementation.java>
</component>
</composite>
代码:
@Test
public void 测试组件调用() {
// TODO 获取组件,并设用
SCADomain domain = SCADomain.newInstance("com/config/simple/echo.composite");
Echo echo= domain.getService(Echo.class, "echoComponent");
echo.print("我是神人");
}
public class EchoImp implements Echo {
public void print(String str) {
System.out.println("str等于 "+str)
服务:
简单例子后,我们知道了,我们可以直接调用使用component的实现,而服务是启动域时或启动节点发布的;
此时我们进行服务的编写:
服务 - 屏蔽一些不常用的功能;
<service>…</service>
主要: interface ,binding,callback
1.interface :
<interface.java interface="com.simple.echo.Echo"></interface.java>
对应的接口:
@Remotable
public interface Echo {
public void print(String str);
}
注意:服务中引用interface,必须要声明远程调用(@Remotable)
2.binging 绑定:
一.正常使用:
<component name="echoComponent">
<implementation.java
class="com.simple.echo.EchoImp" />
<service name="Echo"> // 此处是接口名,当使用组件内部时,一定要这样做
<binding.ws uri="http://localhost:8080/echoComponent"></binding.ws>
</service>
</component>
代码方面:
1.接口要使用 : @Remotable
2.实现要使用 : @Service(Echo.class)
实现提供的多个服务:
@Service(interfaces={HelloService.class, AnotherInterface.class})
public class HelloServiceImpl implements HelloService, AnotherInterface{..}
3. reference 引用:
适用于一个组件引用其他组件处理;
XML : reference标签:
属性: 1.name (必须) – 变量名,必须与类的变量名相同
说明: <reference name="reture" target="EchoReture">
private EchoReture reture = null; // 两处名称必须相同;
2.promote (compusite层时必须) –指定相关的组件实现;
3.target(可选) – 被引用的组件名字 - 组件名/服务名,声明此属性,则是在同compusite调用;
子标签: 1.binding(选定) - 绑定调用的接口或服务,用于调用不再一个域内的服务;
2.interface(选定) - 引用接口,暂时只在回调时用到,其他用处不详
@Reference : 属性: name(可选的) — 注入变量名,默认为Java类的数据成员名;
required(可选的) — 是否必须注入服务。默认为true;
个人理解:相较与spring的IOC很相似;
如:<bean><属性 名字="" ref=""/></bean>
原理:本component内部声明一个引用,必然是引用另一个组件;
相关代码:
<component name="echoComponent">
<implementation.java
class="com.simple.echo.EchoImp" />
<service name="Echo">
<binding.ws uri="http://localhost:8080/echoComponent"></binding.ws>
</service>
<reference name="reture" target="EchoReture">
</reference>
</component>
<component name="EchoReture">
<implementation.java class="com.simple.Client.EchoRetureImp"/>
</component>
java代码:
@Service(Echo.class)
public class EchoImp implements Echo {
private EchoReture reture = null;
@Reference(name="reture") // 重点:设置注入方法;
public void setReture(EchoReture reture) {
this.reture = reture;
}
public void print(String str) {
System.out.println("str等于 "+str);
}
public void printString() {
reture.getEcho();
}
}
其他无用类省略…;
4.property 属性
一个属性可以是简单数据类型或XML可以通过代码实现或者注解;
name(必须)– 定义名字;
many (可选) – 是否多个值, 默认false;
mustSupply (可选) –是否必填,默认false;
source(可选) –引用其他属性 - 使用$XXX
简单例子: 注入字符串:
XML:
<component name="loginCompunent_string">
<implementation.java class="com.simple.property.LogionAction_String" />
<property name="username" >wang</property>
<property name="password">123456</property>
</component>
代码:
public class LogionAction_String {
private String username = null;
private String password = null;
@Property(name="username") // 注入XML配置的值
public void setUsername(String username) {
this.username = username;
}
@Property(name="password") // 注入XML配置的值
public void setPassword(String password) {
this.password = password;
}
public User login() throws Exception {
// TODO 判断输入是否正确,并返回用户对象
// 1.验证传递用户是否符合输入规范;
// 2.记录登录人信息;
// 3.创建用户对象,并赋值;
// 4.返回对象;
/* 1.0 判断用户是否为null */
if(username!=null&&password!=null) {
/* 2.0 登录信息记录 */
System.out.println("用户 : "+username+" 密码 : "+password+" 成功登录");
/* 3.0 创建用户,赋值 */
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;/* 4.0 返回对象 */
} else
throw new NullPointerException("username或password - 不存在");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?