`
log_cd
  • 浏览: 1089647 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring中的远程访问和web服务

阅读更多
一、介绍
    目前,Spring提供对下面四种远程访问技术的支持:
  1. 远程方法调用(RMI):通过使用RmiProxyFactoryBean和RmiServiceExporter,Spring支持传统的RMI(使用java.rmi.Remote interfaces 和 java.rmi.RemoteException)和通过RMI调用器(可以使用任何Java接口)的透明远程调用。
  2. Spring的HTTP调用器:Spring提供一种特殊的远程调用策略支持任何Java接口(象RMI调用器一样),它允许Java序列化能够通过HTTP传送。对应的支持类是HttpInvokerProxyFactoryBean和HttpInvokerServiceExporter。
  3.      和Burlap和Hessian使用自身序列化机制的轻量级协议相反,Spring HTTP调用器使用标准Java序列化机制来通过HTTP输出业务。如果你的参数或返回值是复杂类型,并且不能通过Hessian和Burlap的序列化机制序列化,HTTP调用器就很有优势。Spring可以使用J2SE提供的标准功能或Commons的HttpClient来实现HTTP调用。如果你需要更先进,更好用的功能,就使用后者。
  4. Hessian:通过使用HessianProxyFactoryBean和HessianServiceExporter,你可以使用Caucho提供的轻量级基于HTTP的二进制协议透明地提供你的业务。Hessian提供了一个基于HTTP的二进制远程协议。它由Caucho创建。
  5. Burlap:Burlap是基于XML的,它可以完全代替Hessian。Spring提供的支持类有BurlapProxyFactoryBean和BurlapServiceExporter。 Burlap是Hessian的基于XML实现。
 
二、如何选择?
   当使用RMI时,通过HTTP协议访问对象是不可能的,除非你用HTTP包裹RMI流。RMI是一种很重的协议,因为他支持完全的对象序列化,这样的序列化在要求复杂数据结构在远程传输时是非常重要的。然而,RMI-JRMP只能绑定到Java客户端:它是一种Java-to-Java的远程访问的方案。

    如果你需要基于HTTP的远程访问而且还要求使用Java序列化,Spring的HTTP调用器是一个很好的选择。它和RMI调用器使用相同的基础设施,仅仅使用HTTP作为传输方式。注意HTTP调用器不仅只能用在Java-to-Java的远程访问,而且在客户端和服务器端都必须使用Spring。(Spring为非RMI接口提供的RMI调用器也要求客户端和服务器端都使用Spring)

    当在异构环境中,Hessian和Burlap就非常有用了。因为它们可以使用在非Java的客户端。然而,对非Java支持仍然是有限制的。已知的问题包括含有延迟初始化的collection对象的Hibernate对象的序列化。如果你有一个这样的数据结构,考虑使用RMI或HTTP调用器,而不是Hessian。

    最后但也很重要的一点,EJB优于RMI,因为它支持标准的基于角色的认证和授权,以及远程事务传递。用RMI调用器或HTTP调用器来支持安全上下文的传递是可能的,虽然这不是由核心Spring提供:而是由第三方或在定制的解决方案中插入拦截器来解决的。

三、例子
   定义好用于测试的接口和实现。
package com.logcd.server.service;

public interface IHelloService {   
    public String doHello(String name);   
}

package com.logcd.server.service.impl;

import com.logcd.server.service.IHelloService;

public class HelloService implements IHelloService{
	public String doHello(String name) {
		return "Hello , " + name;   
	}
}

(一)使用Hessian
(1)server端:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
	<servlet>
		<servlet-name>Hessian</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>
	            classpath:Hessian-servlet.xml
	        </param-value>			
		</init-param> 
		<load-on-startup>1</load-on-startup>
	</servlet>  
		           
	<servlet-mapping>
		<servlet-name>Hessian</servlet-name>
		<url-pattern>/hessian/*</url-pattern>
	</servlet-mapping>

</web-app>

(2)server端:Hessian-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"    
  "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans>
	
	<bean id="helloService" class="com.logcd.server.service.impl.HelloService"/>
	
	<bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="helloService"/>
		<property name="serviceInterface">
			<value>
				com.logcd.server.service.IHelloService
			</value>
		</property>
	</bean>
	
</beans>

(3)client端测试:Hessian-client.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
	<property name="serviceUrl">
		<value>http://localhost:8080/hessian/helloService</value>
	</property>
	<property name="serviceInterface">
		<value>com.logcd.server.service.IHelloService</value>
	</property>
</bean>

</beans>

(4)测试程序:
package com.logcd.client.test;

import java.net.MalformedURLException;

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

import com.caucho.hessian.client.HessianProxyFactory;
import com.logcd.server.service.IHelloService;

public class HessianTest {

	public static void main(String args[]){
		clientSpringTest();
	}
	
	public static void clientSpringTest(){

		ApplicationContext context= new ClassPathXmlApplicationContext("Hessian-client.xml");    
		IHelloService helloService = (IHelloService)context.getBean("helloService");   
		System.out.println(helloService.doHello("logcd"));  
       
	}
	
	public static void clientJavaTest(){

		String url = "http://localhost:8080/hessian/helloService"; 
		HessianProxyFactory factory = new HessianProxyFactory(); 
		try {
			IHelloService helloService =(IHelloService)factory.create(IHelloService.class, url);

			System.out.println(helloService.doHello("logcd"));   

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
	}
	
}

(二)使用HTTP调用器
(1)server端:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
	<servlet>
		<servlet-name>httpInvoker</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>
	            classpath:httpinvoker-servlet.xml
	        </param-value>			
		</init-param> 
		<load-on-startup>1</load-on-startup>
	</servlet>  
		           
	<servlet-mapping>
		<servlet-name>httpInvoker</servlet-name>
		<url-pattern>/httpInvoker/*</url-pattern>
	</servlet-mapping>

</web-app>

(2)server端:httpinvoker-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
	"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="helloService" class="com.logcd.server.service.impl.HelloService"/>
    
    <bean name="/helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" lazy-init="false">
        <property name="service">
            <ref bean="helloService"/>
        </property>
        <property name="serviceInterface">
            <value>com.logcd.server.service.IHelloService</value>
        </property>
    </bean>
    
</beans>

(3)client端:httpinvoker-client.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="helloService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

	<property name="serviceUrl">
		<value>http://localhost:8080/httpInvoker/helloService</value>
	</property>

	<property name="serviceInterface">
		<value>com.logcd.server.service.IHelloService</value>
	</property>
	
	<!--
		默认情况下,客户端的HttpInvokerProxy使用J2SE的HTTP Client来建立连接
		org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor
	-->
	<property name="httpInvokerRequestExecutor">
		<bean
	    class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor" />
	</property>
	
</bean>


(4)测试类
package com.logcd.client.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import com.logcd.server.service.IHelloService;

public class HttpInvokerTest {

	public static void main(String args[]){
		clientJavaTest();
	}
	
	public static void clientSpringTest(){

		ApplicationContext context= new ClassPathXmlApplicationContext("httpinvoker-client.xml");    
		IHelloService helloService = (IHelloService)context.getBean("helloService");   
		System.out.println(helloService.doHello("logcd"));  
       
	}
	
	public static void clientJavaTest(){

		String url = "http://localhost:8080/httpInvoker/helloService"; 
		HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean (); 

		factory.setServiceInterface(IHelloService.class);
		factory.setServiceUrl(url);
		factory.afterPropertiesSet();
		
		IHelloService helloService = (IHelloService)factory.getObject();
		System.out.println(helloService.doHello("logcd")); 
		
	}
	
}

四、使用自定义Annotation实现
(1)Custom Annotation Configuration for Spring Remoting
(2)Custom Annotation Configuration for Spring Remoting, Part Two
分享到:
评论

相关推荐

    基于Spring的远程访问与Web Service

    基于Spring的远程访问与Web Service

    Spring 实现远程访问详解——rmi

    Spring远程访问通过使用普通POJOs,能更容易的开发远程访问服务。目前,Spring远程访问的主要技术如下: 1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,...

    Spring 实现远程访问详解——jms和activemq

    前几章我们分别利用spring rmi、httpinvoker、httpclient、webservice技术实现不同服务器间的远程访问。本章我将通过spring jms和activemq实现单Web项目服务器间异步访问和多Web项目服务器间异步访问。 一. 简介 1. ...

    Spring 实现远程访问详解——webservice

    Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice. 1. webservice远程...

    Spring 远程调用 -- C# 访问java WEB 服务

    Spring 远程调用 -- C# 访问java WEB 服务,之前写的Demo,希望对大家有所帮帮助!

    Spring Framework 5 中文文档

    1. 入门指南 2. 介绍Spring框架 3. IoC容器 4. 资源 5. 验证、数据绑定和类型转换 6. Spring表达式语言 ...24. 使用Spring提供远程和WEB服务 25. 整合EJB 26. JMS 28. 使用Spring提供远程和WEB服务 32. 缓存

    基于Spring MVC、CXF和Hibernate的Web服务与数据库操作设计源码

    本源码提供了一个基于Spring MVC、CXF和Hibernate的Web服务与数据库操作设计。...这个系统通过Web服务技术(webservice)实现了对数据库数据的增删查操作,适合需要进行远程数据访问和操作的应用场景。

    spring3.2中文文档

    spring3.2框架是一个轻量级的解决方案和...spring 框架支持声明式事务管理、远程访问 你的逻辑通过RMI或web服务,以及各种选项 坚持你的数据。 它提供了一个功能全面的 MVC框架 ,并允许您 整合 aop 透明地进 你的软件。

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring jar 包详解

    (11) spring-web.jar 这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、 Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 (12) ...

    Spring Boot 实战开发2022年

    │ 开篇词 从零开始:为什么要学习 Spring Boot?.mp4 │ 01 家族生态:如何正确理解 Spring 家族的技术体系?.mp4 │ 02 案例驱动:如何剖析一个 ...│ 24 服务测试:如何使用 Spring 测试 Web 服务层组件?.mp4

    spring4.3.9相关jar包

    spring-context.jar(必须):这个jar 文件在基础IOC功能上为Spring 核心提供了大量扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持...

    Spring in Action(第二版 中文高清版).part2

    8.1 Spring远程调用概览 8.2 与RMI一起工作 8.2.1 连接RMI服务 8.2.2 输出RMI服务 8.3 使用Hessian和Burlap的远程调用 8.3.1 访问Hessian/Burlap服务 8.3.2 用Hessian或Burlap公开Bean的功能 8.4 使用...

    Spring in Action(第二版 中文高清版).part1

    8.1 Spring远程调用概览 8.2 与RMI一起工作 8.2.1 连接RMI服务 8.2.2 输出RMI服务 8.3 使用Hessian和Burlap的远程调用 8.3.1 访问Hessian/Burlap服务 8.3.2 用Hessian或Burlap公开Bean的功能 8.4 使用...

    spring4.1核心包

    在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。这个jar 文件为Spring 核心提供了大量扩展。可以找到使用...

    Spring攻略(第二版 中文高清版).part1

    6.1 在一般Web应用中访问Spring 209 6.1.1 问题 209 6.1.2 解决方案 210 6.1.3 工作原理 210 6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 ...

    最新ssm项目基于spring和vue开发的web新闻流媒体平台+vue.zip

    最新SSM项目基于Spring和Vue开发的Web新闻...通过这些资料,用户可以了解到如何构建一个高效、现代化的基于Spring和Vue的Web新闻流媒体平台,并且可以作为一个实用的案例来学习Spring Boot和Vue.js在实际应用中的使用。

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    因此,就算你只是为了远程访问(例如,暴露Hessian或者 HTTP调用服务)而使用DispatcherServlet,你也得将'spring-webmvc.jar'(或者 'spring-webmvc-portlet/struts.jar')放到'spring.jar'旁边去。 Spring 2.0的 '...

Global site tag (gtag.js) - Google Analytics