上一篇部落格Hessian探究(一)Hessian入門示例我們初步簡單的介紹了一下Hessian的使用入門示例,我們是通過Servlet來暴露Hessian的對外服務的,接下來我們介紹一下通過SpringMVC的方式來暴露Hessian的對外調用服務
(1)在springMVC的配置檔案springmvc-config.xml中進行配置,類似一個普通的Controller,這樣我們就可以通過連接配接來通路Controller了,Hessian和springMVC結合也是類似這樣。這樣對外暴露的連接配接就是http://localhost/hessian.action了。
<bean id="helloService" class="com.tianjunwei.hessian.server.HelloServiceImpl" />
<!-- 使用Spring的HessianServie做代理 -->
<bean name="/hessian.action"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- service引用具體的實作實體Bean-->
<property name="service" ref="helloService" />
<property name="serviceInterface" value="com.tianjunwei.hessian.server.HelloService" />
</bean>
(2)用戶端就可以通路Hessian使用springMVC暴露的服務。
package com.tianjunwei.hessian.client;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.tianjunwei.hessian.server.HelloService;
public class HelloServiceControllerMain {
public static void main(String [] args) throws MalformedURLException{
String url = "http://localhost/hessian.action";
System.out.println(url);
HessianProxyFactory factory = new HessianProxyFactory();
HelloService helloService = (HelloService) factory.create(HelloService.class, url);
System.out.println(helloService.helloWorld("world"));
}
}