spring 集成 Hessian

来源:互联网 发布:淘宝商家规则 编辑:程序博客网 时间:2024/05/29 16:32

简介:Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

本文讲述了spring对Hessian的集成。

服务端:

  maven依赖:

  <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.2.8.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>3.2.8.RELEASE</version>        </dependency>        <dependency>            <groupId>com.caucho</groupId>            <artifactId>hessian</artifactId>            <version>4.0.38</version>        </dependency>    </dependencies>

接口:

public interface AccountService {    public void insertAccount(Account account);    public Account getAccount();}


接口实现类:

public class AccountServiceImpl implements AccountService {    public void insertAccount(Account acc) {        System.out.println(acc.getName());    }    public Account getAccount() {        Account account = new Account();        account.setName("account");        return account;    }}

实体类:

public class Account implements Serializable {    private String name;    public String getName(){        return name;    }    public void setName(String name) {        this.name = name;    }}

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5">    <servlet>        <servlet-name>remoting</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath*:/remoting-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>remoting</servlet-name>        <url-pattern>/remoting/*</url-pattern>    </servlet-mapping>    <!--<context-param>-->        <!--<param-name>contextConfigLocation</param-name>-->        <!--<param-value>-->            <!--classpath*:/applicationContext.xml-->        <!--</param-value>-->    <!--</context-param>-->    <!--<listener>-->        <!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->    <!--</listener>-->    <!--<servlet>-->        <!--<servlet-name>accountExporter</servlet-name>-->        <!--<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>-->    <!--</servlet>-->    <!--<servlet-mapping>-->        <!--<servlet-name>accountExporter</servlet-name>-->        <!--<url-pattern>/remoting/AccountService</url-pattern>-->    <!--</servlet-mapping>--></web-app>

remoting-mvc.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-3.2.xsd">    <bean id="accountService" class="com.yx.serverHessian.AccountServiceImpl">    </bean>   <!--暴露AccountService-->    <bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">        <property name="service" ref="accountService"/>        <property name="serviceInterface" value="com.yx.serverHessian.AccountService"/>    </bean>    <bean id="pwdService" class="com.yx.serverHessian.PwdServiceImpl">    </bean>     <!--暴露PwdService-->    <bean name="/PwdService" class="org.springframework.remoting.caucho.HessianServiceExporter">        <property name="service" ref="pwdService"/>        <property name="serviceInterface" value="com.yx.serverHessian.PwdService"/>    </bean></beans>

  q:这里服务是怎么映射的了?

   a:在servlet.xml没有显示的指定handler mapping,spring默认采用BeanNameUrlHanderMapping 的映射方式,则URL映射过来会自动映射到对应的服务上。


spring还介绍了另一种简单的配置方式:

  将Hessian服务暴露在applicationContext.xml中而不是servlet.xml中,将servlet换成了HttpRequestHandlerServlet。


  配置如下:

  web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5">    <!--<servlet>-->        <!--<servlet-name>remoting</servlet-name>-->        <!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>-->        <!--<init-param>-->            <!--<param-name>contextConfigLocation</param-name>-->            <!--<param-value>classpath*:/remoting-mvc.xml</param-value>-->        <!--</init-param>-->        <!--<load-on-startup>1</load-on-startup>-->    <!--</servlet>-->    <!--<servlet-mapping>-->        <!--<servlet-name>remoting</servlet-name>-->        <!--<url-pattern>/remoting/*</url-pattern>-->    <!--</servlet-mapping>-->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            classpath*:/applicationContext.xml        </param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>accountExporter</servlet-name>        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>accountExporter</servlet-name>        <url-pattern>/remoting/AccountService</url-pattern>    </servlet-mapping></web-app>


applicationContext.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.xsd">    <bean id="accountService" class="com.yx.serverHessian.AccountServiceImpl"/>    <bean name="accountExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">        <property name="service" ref="accountService"/>        <property name="serviceInterface" value="com.yx.serverHessian.AccountService"/>    </bean></beans>

   注:这里applicationContext.xml要暴露的service对应的bean name(accountExporter)必须和web.xml中的servlet name对应

   服务端的这两种配置各有各的优势,在一个以springmvc作为开发的框架里建议采用第一种配置方式,因为如果服务端有多个服务需要部署,则仅需要在servlet.xml中多配置几个服务的映射bean即可。而在servlet.xml中配置服务映射也和servlet的作用相符。如果采用的是其他框架作为请求转发,则可采用第二种配置方式,因为不涉及到servlet的配置,配置仅在applicationContext.xml中,不多还有个弊端多个服务的部署需要在web.xml中配置多有servlet.(仅笔者个人建议)


客户端:

  首先将服务端的service接口类以及实体类弄成jar包以便在客户端使用,当然你也可以把相关java类直接复制过来直接用。

  maven依赖:

      <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.2.8.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>3.2.8.RELEASE</version>        </dependency>        <dependency>            <groupId>com.caucho</groupId>            <artifactId>hessian</artifactId>            <version>4.0.38</version>        </dependency>    </dependencies>

applicationContext.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-3.2.xsd"default-lazy-init="true">    <bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">        <property name="serviceUrl" value="http://localhost:8080/serverHessian/remoting/AccountService"/>        <property name="serviceInterface" value="com.yx.serverHessian.AccountService"/>    </bean>    <!--<bean id="pwdService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">-->        <!--<property name="serviceUrl" value="http://localhost:8080/serverHessian/remoting/PwdService"/>-->        <!--<property name="serviceInterface" value="com.yx.serverHessian.PwdService"/>-->    <!--</bean>--></beans>

    注:一般多个服务的url配置一般都是在在配置文件中先写好配置url后在applicationContext中加上对应的服务,http://localhost:8080/serverHessian/remoting/AccountService

可写成${remoteUrl}/AccountService

   测试的main类:

public class Main {    public static void main(String[] args)    {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        AccountService accountService = (AccountService)applicationContext.getBean("accountService");        Account account = accountService.getAccount();        System.out.println("name: " + account.getName());        accountService.insertAccount(account);//        PwdService pwdService = (PwdService)applicationContext.getBean("pwdService");//        Pwd pwd = pwdService.getPwd();////        System.out.println("name: " + pwd.getPwd());    }}

    注:url请不要写错了http://localhost:8080/serverHessian/remoting/AccountService,这里的serverHessian是服务端应用程序上下文,remoting对应servlet的映射,AccountService对应服务。


    


   


  


0 0