Hessian协议+Spring配置

Maven依赖:

        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.7</version>
        </dependency>

web.xml配置:

    <!-- 配置Spring核心控制器 -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

服务提供方 hessian-service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean name="/hessian/queryUserService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="queryUserService" />
        <property name="serviceInterface" value="com.service.IQueryUserService" />
    </bean>
	
	 <bean name="/hessian/addUserService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="userService" />
        <property name="serviceInterface" value="com.service.IUserService" />
    </bean>
</beans>

服务方spring-mvc.xml配置(将hessian-service提供的接口映射到dispatcherServlet)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">                  
     <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
	<context:component-scan base-package="com.controller">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	</context:component-scan>
	
	 <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />
    <!-- 避免IE在ajax请求时,返回json出现下载 -->
    <bean id="jacksonMessageConverter"  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>
	
	<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
		<property name="prefix" value="WEB-INF/page/" />
		<property name="suffix" value=".jsp" />
	</bean>   
	
	<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
	<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean>
    <import resource="hessian-service.xml"/>        
</beans>

接口调用方hessian-client.xml(将这些bean注册到spring容器内)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 客户端Hessian代理工厂Bean -->
    <bean id="queryUserService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 请求代理Servlet路径 -->      
        <property name="serviceUrl" value="${hessian.serviceUrl}/queryUserService"/>
        <!-- 接口定义 -->
        <property name="serviceInterface" value="com.service.IQueryUserService"/>
    </bean>
		
	<bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl" value="${hessian.serviceUrl}/addUserService"/>
        <property name="serviceInterface" value="com.service.IUserService"/>
    </bean>
</beans>

hesian.properties配置

hessian.serviceUrl=http://127.0.0.1:8082/Provider/hessian

 applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/tx 
                     	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">                                           
        <import resource="spring-import.xml"/>
        <import resource="spring-mybatis.xml"/>
        <import resource="hessian-client.xml"/>
</beans>

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值