使用eclipse建立hessian服務 對于Hessian而言,有服務端和用戶端,是以我們的整合也需要分服務端的整合和用戶端的整合。
hessian服務端源碼結構
hessian 用戶端源碼結構
廢話不多說,實作遠端服務,最重要的是如何使用輪子,下面直接上配置檔案,源代碼:
http://download.csdn.net/download/heisemuyangquan/10272025
server配置 <? 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" xmlns:mvc = "http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > < bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> < bean id = "userService" class = "com.hessian.service.impl.UserServiceImpl" /> < bean name = "/userService" class = "org.springframework.remoting.caucho.HessianServiceExporter" > < property name = "service" ref = "userService" /> < property name = "serviceInterface" value = "com.hessian.service.UserService" /> </ bean > </ beans >
client配置 <? 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" xmlns:mvc = "http://www.springframework.org/schema/mvc" 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 = "userService" class = "org.springframework.remoting.caucho.HessianProxyFactoryBean" > < property name = "serviceUrl" value = "http://localhost:8080/hessianServer/api/service/userService" /> < property name = "serviceInterface" value = "com.hessian.service.UserService" /> </ bean > </ beans >
另外需要注意的是hessian和Spring的版本相容問題
需要導入Spring的相關依賴要在pom中配置
pom.xml僅供參考,親測可用
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd" > < modelVersion > 4.0.0 </ modelVersion > < groupId > com.hiquanten </ groupId > < artifactId > hessianServer </ artifactId > < packaging > war </ packaging > < version > 0.0.1-SNAPSHOT </ version > < name > hessianServer Maven Webapp </ name > < url > http://maven.apache.org </ url > < properties > < spring.version > 4.3.7.RELEASE </ spring.version > </ properties > < dependencies > <!-- https://mvnrepository.com/artifact/com.caucho/hessian --> < dependency > < groupId > com.caucho </ groupId > < artifactId > hessian </ artifactId > < version > 4.0.51 </ version > </ dependency > < dependency > < groupId > junit </ groupId > < artifactId > junit </ artifactId > < version > 4.12 </ version > < scope > test </ scope > </ dependency > < dependency > < groupId > org.springframework </ groupId > < artifactId > spring-core </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 2)這個jar 檔案是所有應用都要用到的,它包含通路配置檔案、建立和管理bean 以及進行Inversion of Control / Dependency Injection(IoC/DI)操作相關的所有類。如果應用隻需基本的IoC/DI 支援,引入spring-core.jar 及spring-beans.jar 檔案就可以了。 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring-beans </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 3)這個jar 檔案為Spring 核心提供了大量擴充。可以找到使用Spring ApplicationContext特性時所需的全部類,JDNI 所需的全部類,instrumentation元件以及校驗Validation 方面的相關類。 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring-context </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 4) 這個jar 檔案包含對Spring 對JDBC 資料通路進行封裝的所有類。 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring- jdbc </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 5) 為JDBC、 Hibernate 、JDO、JPA等提供的一緻的聲明式和程式設計式事務管理。 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring- tx </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 6)Spring web 包含Web應用開發時,用到Spring架構時所需的核心類,包括自動載入WebApplicationContext特性的類、Struts與JSF內建類、檔案上傳的支援類、Filter類和大量工具輔助類。 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring-web </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 7)包含SpringMVC架構相關的所有類。 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring- webmvc </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- 8)Spring test 對JUNIT等測試架構的簡單封裝 --> < dependency > < groupId > org.springframework </ groupId > < artifactId > spring-test </ artifactId > < version > ${spring.version} </ version > < scope > test </ scope > </ dependency > < dependency > < groupId > org.springframework </ groupId > < artifactId > spring- aop </ artifactId > < version > ${spring.version} </ version > </ dependency > <!-- Servlet web --> < dependency > < groupId > javax.servlet </ groupId > < artifactId > javax.servlet- api </ artifactId > < version > 3.1.0 </ version > </ dependency > </ dependencies > < build > < finalName > hessianServer </ finalName > < plugins > < plugin > <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --> < groupId > org.apache.maven.plugins </ groupId > < artifactId > maven -compiler- plugin </ artifactId > < version > 3.6.1 </ version > < configuration > < source > 1.8 </ source > < target > 1.8 </ target > < encoding > UTF8 </ encoding > </ configuration > </ plugin > < plugin > < groupId > org.mortbay.jetty </ groupId > < artifactId > jetty - maven - plugin </ artifactId > < version > 8.1.10.v20130312 </ version > < configuration > < webAppConfig > < contextPath > /HessianSpringMaven </ contextPath > </ webAppConfig > </ configuration > </ plugin > </ plugins > </ build > </ project >