Remoting and web services using Spring[摘自官网]

spring document url:

http://docs.spring.io/spring/docs/

Using Hessian

First we’ll have to create a new servlet in your application (this is an excerpt from ‘web.xml‘):

<servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
</servlet-mapping>

ou’re probably familiar with Spring’s DispatcherServlet principles and if so, you know that now you’ll have to create a Spring container configuration resource named ‘remoting-servlet.xml‘ (after the name of your servlet) in the ‘WEB-INF‘ directory.

Alternatively, consider the use of Spring’s simpler HttpRequestHandlerServlet. This allows you to embed the remote exporter definitions in your root application context (by default in ‘WEB-INF/applicationContext.xml‘), with individual servlet definitions pointing to specific exporter beans. Each servlet name needs to match the bean name of its target exporter in this case.

In the newly created application context called remoting-servlet.xml, we’ll create a HessianServiceExporter exporting your services:

<bean id="accountService" class="example.AccountServiceImpl">
    <!-- any additional properties, maybe a DAO? -->
</bean>

<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">
  <!-- 构造需要的输入参数 -->
  <property name="service" ref="accountService"/>
   <!-- 服务端客户端使用的接口 -->
   <property name="serviceInterface" value="example.AccountService"/>
</bean>

In the latter case, define a corresponding servlet for this exporter in ‘web.xml‘, with the same end result: The exporter getting mapped to the request path/remoting/AccountService. Note that the servlet name needs to match the bean name of the target exporter.

<servlet>
    <servlet-name>accountExporter</servlet-name>
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>accountExporter</servlet-name>
    <url-pattern>/remoting/AccountService</url-pattern>
</servlet-mapping>

to link in the service on the client, we’ll create a separate Spring container, containing the simple object and the service linking configuration bits:

<bean class="example.SimpleObject">
    <property name="accountService" ref="accountService"/>
</bean>

<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
</bean>
//假设不定义SimpleObject
//配置文件名为spring-config-client.xml
//client可以使用这样的代码调用
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");
AccountService accountService = (AccountService)context.getBean("accountService");
//
//POJO for transport
//
public class Account implements Serializable{

    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
//
//server and client
//
public interface AccountService {

    public void insertAccount(Account account);

    public List<Account> getAccounts(String name);

}
//
//server
// the implementation doing nothing at the moment
public class AccountServiceImpl implements AccountService {

    public void insertAccount(Account acc) {
        // do something...
    }

    public List<Account> getAccounts(String name) {
        // do something...
    }

}
//
//client
//
public class SimpleObject {

    private AccountService accountService;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    // additional methods using the accountService

}

org.springframework.remoting.caucho.HessianServiceExporter - This exports the specified service as a servlet based http request handler. Hessian services exported by this class can be accessed by any hessian client.
org.springframework.remoting.caucho.HessianProxyFactoryBean - This is the factory bean for Hessian clients. The exposed service is configured as a spring bean. The ServiceUrl property specifies the URL of the service and the ServiceInterface property specifies the interface of the implemented service.

1.Client sends a message call
2.This message call is handled by a Hessian Proxy created by HessianProxyFactoryBean
3.The Hessian Proxy converts the call into a remote call over HTTP
4.The Hessian Service Adapter created by HessianServiceExporter intercepts the remote call over HTTP
5.It forwards the method call to Service

时间: 2024-10-12 20:09:34

Remoting and web services using Spring[摘自官网]的相关文章

Spring Batch_官网DEMO实现

http://spring.io/guides/gs/batch-processing/ 使用spring xml方式实现了spring batch官网的demo,现在把具体的代码贴出来,具体的细节配置还要参考官网的说明. 首先建立maven项目,pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&

NVIDIA显卡系列(摘自官网)

本文内容摘自nvidia官网 http://www.nvidia.cn/object/geforce_family_cn.html 显卡系列内容如下(摘于2016/8/1) 桌面 桌面 桌面 GeForce GTX Titan Z GeForce GTX Titan X GeForce GTX 950(全新!)GeForce GTX 750 Ti  GeForce GT 730 GeForce GT 710 (全新!) GeForce 210 笔记本 笔记本 笔记本 GeForce GTX 98

spring改版官网下载jar包, 源代码和文档

从网上找了一些方法,现在都整理了一下,有简单粗暴的,也有百转回肠的(详细,直接从官网一步一步的进入下载页),希望大家根据自己的喜好可以找到的真爱. 方法一:(简单粗暴直接) http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-framework-3.2.4.RELEASE-dist.zip 直接粘到地址栏或者下载工具里,每次有更新只要改版本号就可以: 方法二

jQuery Validate (摘自官网)

jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API.所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言. 该插件是由 Jörn Zaefferer 编写和维护的,他是 jQuery 团队的一名成员,是 jQuery UI 团队的主要开发人员,是 QUnit 的维护人员.该插件在 20

Spring 4 官方文档学习 Spring与Java EE技术的集成

本部分覆盖了一下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Enterprise JavaBeans (EJB) integration -- EJB集成 Chapter 30, JMS (Java Message Service) -- JMS (Java 消息服务) Chapter 31, JMX Chapter 32, JCA CCI Chapter 33,

基于Spring设计并实现RESTful Web Services(转)

基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon Web Services的整合,到聚合多个数据源,RESTful网络服务遵从了Roy Fielding的架构风格的指导方针,提供了简单.高效的web APIs,支持的API用户数量从少量到百万级别. 你要实现一个RESTful风格的网络服务,可能会是因为: 你正在创建一个API,而客户端需要通过网

使用 Spring 3 来创建 RESTful Web Services(转)

使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参考实现 Jersey.使用 Restlet 框架和从头开始开发.Spring 是流行的 Java EE 应用开发框架,现在它的 MVC 层也支持 REST 了.本文将介绍使用 Spring 开发 RESTful Web Services 的方法.读者将了解如何使用 Spring API 和注释来开发

Spring RESTful Web Services

转:http://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参考实现 Jersey.使用 Restlet 框架和从头开始开发.Spring 是流行的 Java EE 应用开发框架,现在它的 MVC 层也支持 REST 了.本文将介绍使用 Spring 开发 RESTful Web Services 的方法.读者

使用 Spring 3 来创建 RESTful Web Services

来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java? 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参考实现 Jersey.使用 Restlet 框架和从头开始开发.Spring 是流行的 Java EE 应用开发框架,现在它的 MVC 层也支持 REST 了.本文将介绍使用 Spring 开发 RESTful Web Services 的方法