瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          spring rmi例子

          Posted on 2010-01-27 21:07 瘋狂 閱讀(4071) 評論(0)  編輯  收藏 所屬分類: spring
          首先是服務端:
          接口和實現

          public interface ServerRmiI {

           public String sayHi(String name);
          }


          public class ServerRmiImpl implements ServerRmiI{

           public String sayHi(String name) {
            // TODO Auto-generated method stub
            return "Hi,"+name;
           }

          }

          配置:
          <?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:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"
              default-autowire="byName" default-lazy-init="true">  
               <bean name="rmiserver" class="com.server.ServerRmiImpl"/>
               <bean name="serverTest" class="org.springframework.remoting.rmi.RmiServiceExporter">
                 <property name="service" ref="rmiserver"/>
                 <property name="serviceName" value="serverRmiTest"/>
                 <property name="serviceInterface" value="com.server.ServerRmiI"/>
                 <property name="registryPort" value="1021"/>
               </bean>
          </beans>

          啟動:
          public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml");
                  context.getBean("serverTest"); 
           }
          看見如下信息就代表成功了:
          信息: Binding service 'serverRmiTest' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[yourhost:1021](local),objID:[0:0:0, 0]]]]
          客戶端:
          配置:
          <?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:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"
              default-autowire="byName" default-lazy-init="true">  
               <bean name="clentrmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
               <property name="serviceUrl" value="rmi://yourhost:1021/serverRmiTest"/>
               <property name="serviceInterface" value="com.server.ServerRmiI"/>
               </bean>
          </beans>

          測試類:
          public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml");
                  ServerRmiI rmiI = (ServerRmiI) context.getBean("clentrmi");
                  System.out.println(rmiI.sayHi("rmi"));
           }
          打印:Hi,rmi
          主站蜘蛛池模板: 安溪县| 瑞安市| 廊坊市| 通海县| 广河县| 南岸区| 正蓝旗| 桓台县| 玉田县| 枞阳县| 甘孜县| 庄浪县| 伽师县| 怀仁县| 寻甸| 锡林郭勒盟| 合江县| 桓仁| 丽江市| 恩施市| 冷水江市| 通许县| 石首市| 陈巴尔虎旗| 恭城| 大方县| 莆田市| 赣榆县| 新河县| 岑巩县| 抚宁县| 明水县| 塘沽区| 泌阳县| 通海县| 梁河县| 二手房| 读书| 永丰县| 神农架林区| 泾源县|