Hessian与Spring集成

一、服务端

1、所需jar包(Spring相关jar包、hessian-4.0.37.jar

2、服务端代码

 2.1、实体对象

package com.hessian.spring.entity;

import java.io.Serializable;

public class User implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

  2.2、接口

package com.hessian.spring;

import java.util.List;
import java.util.Map;

import com.hessian.spring.entity.User;

public interface IHello {
	public String sayHello(String name);
	public String getUserList(List<User> users);
	public String getUserMap(Map<String, User> maps);
}

  2.3、实现类

package com.hessian.spring.impl;

import java.util.List;
import java.util.Map;

import com.hessian.spring.IHello;
import com.hessian.spring.entity.User;

public class IHelloImpl implements IHello {

	public String sayHello(String name) {
		return "Hello," + name;
	}

	public String getUserList(List<User> users) {
		StringBuffer stringBuffer = new StringBuffer();
		for (User user : users) {
			stringBuffer.append("[");
			stringBuffer.append(user.getUserName());
			stringBuffer.append("--");
			stringBuffer.append(user.getPassword());
			stringBuffer.append("]");
		}
		return stringBuffer.toString();
	}
	
	public String getUserMap(Map<String, User> maps){
		StringBuffer stringBuffer = new StringBuffer();
		for(String key : maps.keySet()){
			stringBuffer.append("[");
			stringBuffer.append(maps.get(key).getUserName());
			stringBuffer.append("--");
			stringBuffer.append(maps.get(key).getPassword());
			stringBuffer.append("]");
		}
		return stringBuffer.toString();
	}
}

 2.4、配置Web.xml

<!--   Hessian集成Spring-->
<servlet>
	<servlet-name>remote</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>namespace</param-name>
		<param-value>classes/remote-servlet</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>remote</servlet-name>
	<url-pattern>/remote/*</url-pattern>
</servlet-mapping>

 2.5、配置remote-servlet.xml,该文件位于src目录下

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 接口的具体实现类 -->  
    <bean id="iHello" class="com.hessian.spring.impl.IHelloImpl" />  
    <!-- 使用Spring的HessianServie做代理 -->  
    <bean name="/HelloSpring" class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <!-- service引用具体的实现实体Bean-->  
        <property name="service" ref="iHello" />  
        <property name="serviceInterface" value="com.hessian.spring.IHello" />  
    </bean>  
</beans>

 注:

      这个文件为什么叫remote-servlet.xm

     在web.xml中有配置:<servlet-name>remote</servlet-name>

     所以remote-servlet.xml的文件名必须以

二、客户端

1、配置客户端 remote-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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <!-- 客户端Hessian代理工厂Bean -->
	<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<!-- 请求代理Servlet路径 -->
		<property name="serviceUrl">
			<value>http://127.0.0.1:8080/remote/HelloSpring</value>
		</property>
		<!-- 接口定义 -->
		<property name="serviceInterface">
			<value>com.hessian.spring.IHello</value>
		</property>  
	</bean>
</beans>

 2、客户端测试类

package com.hessian.spring.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hessian.spring.IHello;
import com.hessian.spring.entity.User;

public class SpringClientTest {

	public static String url = "http://127.0.0.1:8080/remote/HelloSpring";

	public static void main(String[] args) {
		try {
			ApplicationContext contex = new ClassPathXmlApplicationContext("remote-client.xml");  
	  
	        // 获得客户端的Hessian代理工厂bean  
	        IHello iHello = (IHello) contex.getBean("helloService");
			System.out.println(iHello.sayHello("tzz"));
			User user1 = new User();
			user1.setUserName("a1");
			user1.setPassword("123456");
			User user2 = new User();
			user2.setUserName("a2");
			user2.setPassword("123456");
			List<User> users = new ArrayList<User>();
			users.add(user1);
			users.add(user2);
			System.out.println(iHello.getUserList(users));
			Map<String, User> maps = new HashMap<String, User>();
			maps.put("user1", user1);
			maps.put("user2", user2);
			System.out.println(iHello.getUserMap(maps)); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自tzz6.iteye.com/blog/2262983