zoukankan      html  css  js  c++  java
  • 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

    给笨笨的自己提个醒>_<~
  • 相关阅读:
    python基础26——派生&多态&绑定与非绑定方法
    python基础25——继承&属性查找&多继承的菱形问题&Mixins机制
    emmm......就当练习了系列20
    python基础24——封装&property
    emmm......就当练习了系列19
    python基础23——面向对象
    [转]N年Python老司机,血泪总结新手常见10大错误
    ATM机+购物车
    python基础22——logging模块&
    emmm......就当练习了系列18
  • 原文地址:https://www.cnblogs.com/ephuizi/p/5129024.html
Copyright © 2011-2022 走看看