HessianServiceExporter

使用HessianServiceExporter提供bean服务

根据上面的描述,必须提供remoting-servlet.xml文件。通常,在remoting-servlet.xml文件里定义Hessian服务即可。Spring使用HessianServiceExporter,将一个常规bean导出成Hessian服务。类似于RmiServiceExporterHessianServiceExporter可将一个普通bean导出成远程服务。remoting-servlet.xml的详细配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--  Spring配置文件的文件头,包含dtd信息-->

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

         <!--  定义普通bean实例-->

    <bean id="helloService" class="lee.HelloImpl"/>

         <!--  使用HessianServiceExporter 将普通bean导出成Hessian服务-->

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

                   <!--  需要导出的目标bean-->

           <property name="service" ref="helloService"/>

                   <!--  Hessian服务的接口-->

           <property name="serviceInterface" value="lee.Hello"/>

    </bean>

</beans>

在该配置下, Hessian 服务的 urlhttp://localhost:8888/hessian-spring/remoting/helloService ,其中 hessian-spring 是该应用的 urlSpring 使用 DispatcherServlet 拦截到匹配 /remoting/* 的请求,然后将该请求转发到对应的 bean ,该 beanremoting-servlet.xml 文件中以 HessianServiceExporter 定义。
Hessian是一种轻量级的二进制RPC协议,它可以使得Java和其他语言之间的通信变得更加简单和高效。下面是一个简单的Hessian服务端和客户端的例子: 服务端(Java): 1. 首先,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> ``` 2. 创建一个实现了Hessian服务接口的类,例如: ```java public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 3. 在Spring配置文件中添加以下内容: ```xml <bean id="helloService" class="com.example.HelloServiceImpl"/> <bean id="hessianServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="helloService"/> <property name="serviceInterface" value="com.example.HelloService"/> </bean> ``` 4. 启动应用程序,服务端就可以监听来自客户端的请求了。 客户端(C#): 1. 首先,需要在项目中添加对HessianSharp.dll的引用。 2. 创建一个HessianProxyFactory对象,并使用它来创建一个HelloService接口的代理对象,例如: ```csharp HessianProxyFactory factory = new HessianProxyFactory(); HelloService service = (HelloService)factory.Create(typeof(HelloService), "http://localhost:8080/helloService"); ``` 3. 调用代理对象的方法,例如: ```csharp string result = service.sayHello("world"); Console.WriteLine(result); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值