一、服务端
定义为war工程,使用dispatcherServlet作为发布服务的servlet
web.xml 配置
配置dispatcherServlet的映射路径,制定服务url规则,对于匹配路径规则的url自动找到对应的服务类处理
<!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>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
</web-app>
Spring配置文件中配置服务
配置具体的url映射的服务
配置文件的定义有两种做法
1. 定义文件名称为 [servletname]-servlet.xml ,这里的参数servletname是指配置的dispatcherServlet的名称。
文件放在src/main/webapp/WEB-INF路径下, 这里定义了一个新的应用上下文,它是被ContextLoaderListener加载的上下文的子上下文,本案例采用的是这种方案。
2. 直接在resources文件夹下配置文件,将文件的位置配置到上面的contextConfigLocation参数上,交由ContextLoaderListener加载
<?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.xsd">
<bean id="httpRequestHandlerAdapter" class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean id="/HelloWorldService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloWorldService"/>
<property name="serviceInterface" value="com.github.skosmalla.hello.world.spring.hessian.HelloWorld"/>
</bean>
<bean id="/PrintService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="printService"/>
<property name="serviceInterface" value="com.github.skosmalla.hello.world.spring.hessian.PrintSomeThing"/>
</bean>
</beans>
说明
1. BeanNameUrlHandlerMapping 是当bean的name为“/”开头定义为url请求
2. HttpRequestHandlerAdapter 在这里面非常关键,没有它可能会出现这种错误:
-- No adapter for handler Does your handler implement a supported interface like Controller ?
HttpRequestHandlerAdapter它的作用就是让spring-mvc放出权限给下面的Exporter自己处理整个HTTP 流程
3. HessianServiceExporter这个类是将服务发布出去,有两个属性,service是指定实现类的bean,serviceInterface则是配置为接口的全类名。
注意:展示的配置文件,本例中是采用第一种方式定义的,定义为hessian-servlet.xml,通过ref引用父上下文中的实现类的bean的,实现类的bean定义在contextConfigLocation定义的上下文中。
二、客户端
hessian-config.xml
配置url http://ip:端口/上下文/hessian/* 和接口名,获取代理对象
<?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.xsd">
<bean id="helloWorldService"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8080/hessian/HelloWorldService" />
<property name="serviceInterface"
value="com.github.skosmalla.hello.world.spring.hessian.HelloWorld" />
</bean>
<bean id="printService"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8080/hessian/PrintService" />
<property name="serviceInterface"
value="com.github.skosmalla.hello.world.spring.hessian.PrintSomeThing" />
</bean>
</beans>
测试
package com.github.skosmalla.hello.world.spring.hessian;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by zhouy on 2018/12/4
*/
public class HessianClient {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("hessian-config.xml");
HelloWorld service = (HelloWorld) appContext.getBean("helloWorldService");
String welcomeMessage = service.welcome();
System.out.println(welcomeMessage);
PrintSomeThing pn = (PrintSomeThing) appContext.getBean("printService");
String str = pn.print();
System.out.println(str);
}
}
完整案例下载:https://download.csdn.net/download/ditto_zhou/10826678
参考: http://gogomarine.iteye.com/blog/741209