码迷,mamicode.com
首页 > 编程语言 > 详细

基于Spring的远程调用的实现

时间:2017-03-01 14:58:15      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:spring 远程方法调用

    在Spring remoting中包含三种实现方式:http-invoker,hessian,burlap。具体的配置如下:

一、服务器端配置(applicationContext.xml):

<pre name="code"><?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:mvc="http://www.springframework.org/schema/mvc"

        xmlns:context="http://www.springframework.org/schema/context"

       

        xsi:schemaLocation="

               http://www.springframework.org/schema/beans

               http://www.springframework.org/schema/beans/spring-beans.xsd

               http://www.springframework.org/schema/mvc

               http://www.springframework.org/schema/mvc/spring-mvc.xsd

               http://www.springframework.org/schema/context

               http://www.springframework.org/schema/context/spring-context.xsd">

              

        <context:component-scan base-package="com.plateno.service" />

       

        <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

        <!-- 类似rmi方式,采用java序列化,数据量较大 -->

        <bean name="/httpInvokerService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">

               <property name="service" ref="remoteService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

        <!-- 采用hessian序列化,数据量最小 -->

        <bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">

               <property name="service" ref="remoteService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

        <!-- xml传输数据,数据量比httpinvoker方式小 -->

        <bean name="/burlapService" class="org.springframework.remoting.caucho.BurlapServiceExporter">

               <property name="service" ref="remoteService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean> 

       

        <mvc:interceptors>

               <mvc:interceptor>

                       <mvc:mapping path="/**"/>

                       <bean class="com.plateno.interceptor.CommonInterceptor"/>

               </mvc:interceptor>

        </mvc:interceptors>

       

</beans>

 

二、服务器端配置(web.xml)

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns="http://java.sun.com/xml/ns/javaee"

        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

        id="WebApp_ID" version="2.5">

       

        <context-param>

               <param-name>contextConfigLocation</param-name>

               <param-value>classpath:applicationContext.xml</param-value>

        </context-param>

        <listener>

               <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

        </listener>

       

        <servlet>

               <servlet-name>springmvc</servlet-name>

                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

               <init-param>

                       <param-name>contextConfigLocation</param-name>

                       <param-value>classpath:spring-mvc.xml</param-value>

               </init-param>

        </servlet>

       

        <servlet-mapping>

               <servlet-name>springmvc</servlet-name>

               <url-pattern>/</url-pattern>

        </servlet-mapping>

       

</web-app>

三、写一个service,再写一个serviceImpl就ok了。

 

四、客户端配置:

<pre name="code"><bean id="httpInvokerProxyFactoryBean" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

               <property name="serviceUrl" value="http://localhost:9999/server/httpInvokerService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

       

        <bean id="hessianProxyFactoryBean" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

               <property name="serviceUrl" value="http://localhost:9999/server/hessianService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

       

        <bean id="burlapProxyFactoryBean" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">

               <property name="serviceUrl" value="http://localhost:9999/server/burlapService"/>

               <property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>

        </bean>

 

五、测试类:

@Autowired

        private RemoteService remoteService;

        @RequestMapping(value="/test")

        public ModelAndView test(@RequestParam(value="name", required=false) String name) {

               UserDTO dto = remoteService.getUser("hello");

               System.out.println(dto);

               Map<String, Object> model = new HashMap<String, Object>();

               return new ModelAndView("test/hello", model);

        }

 

    

本文出自 “12547096” 博客,请务必保留此出处http://12557096.blog.51cto.com/12547096/1902200

基于Spring的远程调用的实现

标签:spring 远程方法调用

原文地址:http://12557096.blog.51cto.com/12547096/1902200

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!