Spring4整合Hessian4(MavenWeb实例)

这是一个Spring4与Hessian4整合的示例,包括服务端和客户端实现。项目基于Maven构建,适用于Eclipse环境。文章提供了项目结构、依赖库、关键代码及测试执行步骤。源码可在指定链接下载,测试运行展示了服务调用的效果。
摘要由CSDN通过智能技术生成

一个Spring整合Hessian的Demo,同时包含Hessian服务端与客户端。是一个Maven工程,IDE使用的Eclipse,运行前需要安装Eclipse的Maven插件。源码下载地址:http://download.csdn.net/detail/julyness/9169837

项目结构


整体:


这里写图片描述
Src


这里写图片描述
Webapp:


这里写图片描述
其他目录无需关注

依赖库

Jdk1.6.0_20或以上版本+
这里写图片描述

项目代码

SayHello.java

package com.zlzf.hessianspringmaven.common;

public interface SayHello {
     public String sayHello(String name);
}

SayHelloImpl.java


package com.zlzf.hessianspringmaven.server;

import com.zlzf.hessianspringmaven.common.SayHello;

public class SayHelloImpl implements SayHello{

    @Override   public String sayHello(String name) {       System.out.println("服务端方法被调用!");        return  "Hello,"+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"
    id="WebApp_ID" version="2.5">
    <display-name>HessianSpringMaven Web Application</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>hessianServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:server/hessian-server.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessianServlet</servlet-name>
        <url-pattern>/hessian/*</url-pattern>
    </servlet-mapping>
</web-app>

hessian-server.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="sayHelloImpl" class="com.zlzf.hessianspringmaven.server.SayHelloImpl"></bean>
    <bean name="/sayHello" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="sayHelloImpl" />
        <property name="serviceInterface" value="com.zlzf.hessianspringmaven.common.SayHello" />
    </bean>

</beans>

hessian-client.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 客户端Hessian代理工厂Bean -->
    <bean id="hessianClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 请求代理Servlet路径 -->      
        <property name="serviceUrl">
            <value>http://localhost:8080/HessianSpringMaven/hessian/sayHello</value>
        </property>
        <!-- 接口定义 -->
        <property name="serviceInterface">
            <value>com.zlzf.hessianspringmaven.common.SayHello</value>
        </property>
    </bean>

</beans>

NormalClient.java

package com.zlzf.hessianspringmaven.client;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;
import com.zlzf.hessianspringmaven.common.SayHello;


public class NormalClient {

    public static void main(String[] args) throws MalformedURLException {  
        //Spring Hessian代理Servelet  
        String url = "http://localhost:8080/HessianSpringMaven/hessian/sayHello";  
        HessianProxyFactory factory = new HessianProxyFactory();  
        SayHello api = (SayHello) factory.create(SayHello.class, url);  

        System.out.println(api.sayHello("masongchao"));  
    }  

}

SpringClient.java

package com.zlzf.hessianspringmaven.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zlzf.hessianspringmaven.common.SayHello;



public class SpringClient {
    public static void main(String[] args) {  
         ApplicationContext contex = 
                 new ClassPathXmlApplicationContext("client/hessian-client.xml");
        // 获得客户端的Hessian代理工厂bean  
        SayHello i = (SayHello) contex.getBean("hessianClient");  
        System.out.println(i.sayHello("masongchao"));  
    }  
}

Pom.xml
pom文件中,jetty插件必须配置contextPath,否则通过http://localhost:8080/工程名/index.jsp的形式依然无法访问到index.jsp,提示404。

<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.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlzf.hessianspringmaven</groupId>
  <artifactId>HessianSpringMaven</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HessianSpringMaven Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.caucho</groupId>
        <artifactId>hessian</artifactId>
        <version>4.0.38</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>HessianSpringMaven</finalName>
    <plugins>
        <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>

测试执行

右键工程,Run as,Run Configurations,右键Maven Build,New,命名HessianSpringMaven,配置如图
这里写图片描述
点击右下角的Run。
运行SpringClient,结果如图
这里写图片描述
运行NormalClient,结果如图
这里写图片描述
NormalClient与SpringClient功能相同,只是NormalClient未使用Spring而已。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值