hessian spring实例整合
生活随笔
收集整理的這篇文章主要介紹了
hessian spring实例整合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Hessian是一個輕量級的remoting onhttp工具,使用簡單的方法提供了RMI的功能。 相比WebService,Hessian更簡單、快捷。采用的是二進制RPC協議,因為采用的是二進制協議,所以它很適合于發送二進制數據。
架構
1.hessian-api
hessian-api用于存放對外暴露接口;package com.xiaofeng.hessian.api;/*** @author zhangweixiang* @desc 一句話來描述這個類的作用* @date $date$ $time$*/ public interface UserService {void addUser();void updateUser();void delUser();String findUser(String username); }
2.hessian-server hessian-server服務端
服務實現類: package com.xiaofeng.hessianserver.service;import com.xiaofeng.hessian.api.UserService;/*** @author zhangweixiang* @desc 一句話來描述這個類的作用* @date $date$ $time$*/ public class UserServiceImpl implements UserService {public void addUser() {System.out.println("-------------invoke addUser()---------------");}public void updateUser() {System.out.println("-------------invoke updateUser()---------------");}public void delUser() {System.out.println("-------------invoke delUser()---------------");}public String findUser(String username) {System.out.println("-------------invoke findUser---------------");return "return: " + username;}}
web.xml <!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><servlet><servlet-name>hessianServer</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/spring/springmvc-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>hessianServer</servlet-name><url-pattern>/api/service/*</url-pattern></servlet-mapping></web-app>
spring-servlet.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"><bean id="userService" class="com.xiaofeng.hessianserver.service.UserServiceImpl" /><bean name="/userService"class="org.springframework.remoting.caucho.HessianServiceExporter"><property name="service" ref="userService" /><property name="serviceInterface" value="com.xiaofeng.hessian.api.UserService" /></bean><!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><bean id="userService" class="com.xiaofeng.hessianserver.service.impl.UserServiceImpl" /><bean name="/userService"class="org.springframework.remoting.caucho.HessianServiceExporter"><property name="service" ref="userService" /><property name="serviceInterface" value="com.xiaofeng.hessianserver.service.UserService" /></bean>--></beans>
3.hessian-client hessian-client客戶端調用
import com.xiaofeng.hessian.api.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author zhangweixiang* @desc 一句話來描述這個類的作用* @date $date$ $time$*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/springmvc-servlet.xml") public class HessianTest {@Autowiredprivate UserService userService;@Testpublic void test() {userService.addUser();userService.updateUser();userService.delUser();String user = userService.findUser("ZhangSan");System.out.println(user);System.out.println("--------- ------------------------finished----------------------------------");} }
啟動服務端,然后通過客戶端test調用即可
總結
以上是生活随笔為你收集整理的hessian spring实例整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光大信托有不兑付的项目吗
- 下一篇: springboot 常用配置之多环境配