您的位置:首页 > 编程语言 > Java开发

通过Spring使用远程访问和web服务

2011-09-06 22:50 465 查看
Spring提供类用于集成各种远程访问技术。这种对远程访问的支持可以降低你在用POJO实现支持远程访问业务时的开发难度。目前,Spring提供对下面四种远程访问技术的支持:

*远程方法调用(RMI)。通过使用RmiProxyFactoryBean和RmiServiceExporter,Spring支持传统的RMI(使用java.rmi.Remote interfaces 和 java.rmi.RemoteException)和通过RMI调用器(可以使用任何Java接口)的透明远程调用。

*Spring的HTTP调用器。Spring提供一种特殊的远程调用策略支持任何Java接口(象RMI调用器一样),它允许Java序列化能够通过HTTP传送。对应的支持类是HttpInvokerProxyFactoryBean和HttpInvokerServiceExporter。

*Hessian。通过使用HessianProxyFactoryBean和HessianServiceExporter,你可以使用Caucho提供的轻量级基于HTTP的二进制协议透明地提供你的业务。

*Burlap。Burlap是基于XML的,它可以完全代替Hessian。Spring提供的支持类有BurlapProxyFactoryBean和BurlapServiceExporter。

*JAX RPC (TODO).

下面简单讨论使用HessianHTTP远程调用业务。

Hessian简要介绍:

The Hessian binary web service protocol makes web services usable without requiring a largeframework, and without learning
yet another alphabet soup of protocols. Because it is a binary protocol, it

is well-suited to sending binary data without any need to extend the protocol with attachments.

org.springframework.remoting.caucho

Class HessianProxyFactoryBean

java.lang.Object

org.springframework.remoting.support.RemoteAccessor

org.springframework.remoting.support.UrlBasedRemoteAccessor

org.springframework.remoting.caucho.HessianClientInterceptor

org.springframework.remoting.caucho.HessianProxyFactoryBean

一. 为Hessian建立DispatcherServlet

Hessian使用一个特定的servlet来通过HTTP通讯。使用Spring的DispatcherServlet概念,你可以很容易地创建这样的servlet来提供你的业务。首先我们必须在你的应用中创建一个新的servlet(下面来自web.xml):

<servlet>

<servlet-name>remote</servlet-name>

<servletclass>org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

你可能熟悉Spring的DispatcherServlet概念,如果是的话,你得在WEB-INF目录下建立一个应用上下文,remoteservlet.xml 。这个应用上下文会在下一节中使用。

二. 使用HessianServiceExporter提供你的bean

在这个新的应用上下文remote-servlet.xml中,我们将创建一个HessianServiceExporter来输出你的业务:

<bean id="myService" class="example.myServiceImpl">

<!-- any additional properties, maybe a DAO? -->

</bean>

<bean name="/myService" class="org.springframework.remoting.caucho.HessianServiceExporter">

<property name="service"><ref bean="myService"/></property>

<property name="serviceInterface">

<value>example.myService</value>

</property>

</bean>

现在我们准备在客户端连接这个业务。我们使用BeanNameUrlHandlerMapping,就不需要指定处理器映射将请求(url)映射到业务上,因此业务提供在http://HOST:8080/myService上。

三.客户端连接业务:

我们在客户端使用HessianProxyFactoryBean来连接业务。我们将创建一个单独的bean工厂或应用上下文.然后使用接口就象调用bean一样简单了!

具体操作步骤如下:

1、引入jar包--hessian-3.0.13.jar。

2、在xml文件中配置用于实现接口的bean。

<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

<property name="serviceInterface">

<value>src.example.myService</value>

</property>

<property name="serviceUrl">

<value>http://HOST:8080/myService</value>

</property>

</bean>

<bean id="myBO" class="src.example.MyBOImpl" singleton="true">

<property name="myService"><ref bean="myService"/></property>

</bean>

3、在本地新建一个名为ImyService的接口。并在该接口中声明要调用的函数。

package src.example;

/*站内信接口服务*/

public interface ImyService{

public void Hello(String userid, String username);

}

4、在 MyBOImpl.java中,建函数doHello。在该函数中调用myService.Hello("100","lili"),并注入ImyService实例对象。



private ImyService myService;

public void myService(ImyService myService{

this.myService=myService;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: