SpringMVC整合Hessian简要说明

一、服务端

web.xml 配置

Spring配置文件中配置服务

配置文件的定义有两种做法

说明

二、客户端

hessian-config.xml

测试


一、服务端

定义为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 

          https://blog.csdn.net/isea533/article/details/45038779

          https://blog.sandra-parsick.de/2012/11/12/spring-web-application-with-hessian-services-as-a-maven-project/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值