Hessian初步使用(配合spring)
2015-09-07 17:04
459 查看
首先Hessian是一个非常优秀的框架,仅支持Post请求.
一,准备环境
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
二,代码结构
1,服务端
看官可以简单的做两个接口和实现类,贴出接口,实现类就不写了.
hessian.xml
web.xml
(此处注意一个问题:就是如果不在springMVC的地方配置Init-param,则需要在web-inf下声明一个[servlet-name]-servlet.xml,servlet-name就是
至此,服务端简单的就配置好了!
2,客户端
简单:直接将服务端的接口代码复制进客户端工程,只需复制接口,但要保证和服务端的包类结构一致.
复杂:将接口服务打包放进客户端工程.(maven打jar包)
结构图:
spring.xml
HessianClient.java
一,准备环境
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
二,代码结构
1,服务端
看官可以简单的做两个接口和实现类,贴出接口,实现类就不写了.
package com.mycode.hessian; public interface IHello { public String sayHello(); }
hessian.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 定义普通bean实例 --> <bean id="hello" class="com.mycode.hessian.Hello" /> <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 --> <bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- 需要导出的目标bean --> <property name="service" ref="hello" /> <!-- Hessian服务的接口 --> <property name="serviceInterface" value="com.mycode.hessian.IHello" /> </bean> <!-- ICount接口 --> <bean id="count" class="com.mycode.hessian.Count"/> <bean name="/count" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="count"/> <property name="serviceInterface" value="com.mycode.hessian.ICount" /> </bean> </beans>
web.xml
(此处注意一个问题:就是如果不在springMVC的地方配置Init-param,则需要在web-inf下声明一个[servlet-name]-servlet.xml,servlet-name就是
<span style="font-size:18px;">DispatcherServlet的servlet名字)</span>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>hessian</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/hessian.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- Exceptions page --> <error-page> <error-code>405</error-code> <location>/WEB-INF/405.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/500.jsp</location> </error-page> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/WEB-INF/500.jsp</location> </error-page> </web-app>
至此,服务端简单的就配置好了!
2,客户端
简单:直接将服务端的接口代码复制进客户端工程,只需复制接口,但要保证和服务端的包类结构一致.
复杂:将接口服务打包放进客户端工程.(maven打jar包)
结构图:
spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="helloClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://test.bai.com/hello</value> </property> <property name="serviceInterface"> <value>com.mycode.hessian.IHello</value> </property> </bean> <bean id="countClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://test.bai.com/count</value> </property> <property name="serviceInterface"> <value>com.mycode.hessian.IHello</value> </property> </bean> </beans>
HessianClient.java
<span style="font-size:18px;">package com.mycode.client; import java.net.MalformedURLException; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.caucho.hessian.client.HessianProxyFactory; import com.mycode.hessian.ICount; import com.mycode.hessian.IHello; public class HessianClient { private HessianProxyFactory factory; @Before public void init(){ factory = new HessianProxyFactory(); } @Test public void testHello() throws MalformedURLException { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); IHello hello = (IHello) context.getBean("helloClient"); System.out.println(hello.sayHello()); } @Test public void testCount() throws MalformedURLException{ String urlName = "http://test.secoo.com/count"; ICount count = (ICount) factory.create(ICount.class, urlName); int re = count.countAdd(2, 5); System.out.println(re); } }</span>2种调用,第一种常用,
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 列举PHP的Yii 2框架的开发优势
- hessian 在PHP中的使用介绍
- Windows窗体的.Net框架绘图技术实现方法
- 浅谈JavaScript 框架分类
- 轻量级javascript 框架Backbone使用指南
- javascript实现框架高度随内容改变的方法
- JS刷新框架外页面七种实现代码
- 超赞的动手创建JavaScript框架的详细教程
- asp.net4.0框架下验证机制失效的原因及处理办法
- 插件管理框架 for Delphi(二)
- 零基础学习AJAX之AJAX框架
- Ajax 框架学习笔记
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- Flex中最好的MVC框架Mate框架