spring 远程调用

spring远程调用:
------------------------
-----------server-----------
1.创建web项目
2.添加spring类库
    org.springframework.aop-3.1.0.RELEASE.jar
    org.springframework.asm-3.1.0.RELEASE.jar
    org.springframework.beans-3.1.0.RELEASE.jar
    org.springframework.context-3.1.0.RELEASE.jar
    org.springframework.context.support-3.1.0.RELEASE.jar
    org.springframework.core-3.1.0.RELEASE.jar
    org.springframework.web-3.1.0.RELEASE.jar
    org.springframework.web.servlet-3.1.0.RELEASE.jar
    org.springframework.expression-3.1.0.RELEASE.jar

    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.apache.commons.logging-1.1.1.jar
3.创建接口和实现类
    public interface WelcomeService {
        public void sayHello(String name);
    }

    public class WelcomeServiceImpl implements WelcomeService {
        public void sayHello(String name) {
            System.out.println(name);
        }
    }
4.配置spring配置文件.
    [web-inf/${servler-name}-servlet.xml]
    <?xml version="1.0"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
        
        <!-- pojo -->
        <bean id="welcomeService" class="cn.itcast.spring.rpc.service.WelcomeServiceImpl" />
        
        <!-- 导出器,将pojo对象转变成controller,处理请求 -->
        <bean name="/ws.service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="serviceInterface">
                <value>cn.itcast.spring.rpc.service.WelcomeService</value>
            </property>
            <property name="service" ref="welcomeService" />
        </bean>
    </beans>

-----------client-----------
5.创建java项目
6.引入类库
    同服务端.
7.复制服务端接口到客户端.
8.创建src/client.xml spring配置文件
    <?xml version="1.0"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
        <!-- 客户端代理 -->
        <bean id="wsClient" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
            <property name="serviceUrl">
                <value>http://localhost:8080/lsn_springrpc_0909_server/ws.service</value>
            </property>
            <property name="serviceInterface">
                <value>cn.itcast.spring.rpc.service.WelcomeService</value>
            </property>
        </bean>
    </beans>
9.创建测试类
    public class App {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("client.xml");
            WelcomeService ws = (WelcomeService) ac.getBean("wsClient");
            ws.sayHello("tom");
        }
    }
10.运行
    先启动服务器端
    在启动客户端.

将项目中的统计服务对外公开,供第三方系统整合
-------------------------------------------
--------- server ---------
1.引入类库
    org.springframework.web.servlet-3.1.0.RELEASE.jar
2.配置web.xml文件DispatcherServlet
    <!--  配置spring远程调用使用的分发器servlet -->
    <servlet>
        <servlet-name>service</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:service-spring-http-inovker.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>service</servlet-name>
        <url-pattern>/*.service</url-pattern>
    </servlet-mapping>
3.配置spring远程调用配置文件
    [conf/service-spring-http-invoker.xml]
    <?xml version="1.0"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ">
        
        <!-- 远程调用统计服务类 -->
        <bean name="/ss.service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
            <property name="serviceInterface" value="cn.itcast.surveypark.service.StatisticsService" />
            <property name="service" ref="statisticsService" />
        </bean>
    </beans>

-------  client  ----------
4.添加客户端配置bean.
    <!-- 客户端代理 -->
    <bean id="ssClient" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl">
            <value>http://localhost:8000/lsn_surveypark0909/ss.service</value>
        </property>
        <property name="serviceInterface">
            <value>cn.itcast.surveypark.service.StatisticsService</value>
        </property>
    </bean>
5.测试

 

posted @ 2014-05-22 11:47  放手风筝  阅读(434)  评论(0编辑  收藏  举报