您的位置:首页 > 编程语言 > Java开发

Spring4整合Hessian4(MavenWeb实例)

2015-10-10 14:33 387 查看
一个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-c ee26
ontext.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而已。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息