1 安装tuscany插件 http://archive.apache.org/dist/incubator/tuscany/java/sca/1.2-incubating/updatesite/
2 建立一个java项目hello
定义接口
package soasample;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface Hello {
String say(String name);
}
定义实现类
package soasample;
public class HelloImpl implements Hello {
@Override
public String say(String name) {
// TODO Auto-generated method stub
System.out.println("Hello-->"+ "hello:" + name);
return "hello:" + name;
}
}
定义文件hello.composite文件
<?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://hello" targetNamespace="http://hello" name="hello">
<component name="Hello">
<implementation.java class="soasample.HelloImpl"></implementation.java>
<service name="Hello">
<interface.java interface="soasample.Hello"></interface.java>
<binding.ws></binding.ws>
</service>
</component>
</composite>
3 建立一个java项目first
定义接口
package soasample;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface First {
String first();
}
定义实现
package soasample;
import org.osoa.sca.annotations.Reference;
public class FirstImpl implements First {
private Hello hello = null;
@Override
public String first() {
String say = hello.say("test");
System.out.println("First-->"+ say);
return say;
}
public Hello getHello() {
return hello;
}
@Reference
public void setHello(Hello hello) {
this.hello = hello;
}
}
定义文件first.composite文件
<?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://first"
targetNamespace="http://first"
name="first">
<component name="First">
<implementation.java class="soasample.FirstImpl">
</implementation.java>
<reference name="hello" target="Hello">
<interface.java interface="soasample.Hello"></interface.java>
<binding.ws uri="http://192.168.204.1:8100/Hello"/>
<!--此处ip端口不同,注意看控制台信息修改一下-->
</reference>
<service name="First">
<interface.java interface="soasample.First"></interface.java>
<binding.ws></binding.ws>
</service>
</component>
</composite>
4 启动,先启动hello项目,选中hello.composite文件 运行选择tuscany方式,启动好之后以同样方式启动first项目
你可以看到eclipse启动了3个控制台
OK,你可以以web service方式调用First,看看输出
hello控制台
Hello-->hello:test
first控制台
First-->hello:test
5 tuscany比spring等IOC容器更提升了一步,将协议层彻底分离