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.測試
相關文章
相關標籤/搜索