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 users);
	public String getUserMap(Map 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 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 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



	remote
	org.springframework.web.servlet.DispatcherServlet
	
		namespace
		classes/remote-servlet
	
	1


	remote
	/remote/*

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



      
      
      
      
          
          
          
      

 注:

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

     在web.xml中有配置:remote

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

 

 

二、客户端

1、配置客户端 remote-client.xml



    
    
	
		
		
			http://127.0.0.1:8080/remote/HelloSpring
		
		
		
			com.hessian.spring.IHello
		  
	

 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 users = new ArrayList();
			users.add(user1);
			users.add(user2);
			System.out.println(iHello.getUserList(users));
			Map maps = new HashMap();
			maps.put("user1", user1);
			maps.put("user2", user2);
			System.out.println(iHello.getUserMap(maps)); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

你可能感兴趣的:(Hessian,spring)