瘋狂

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

          spring rmi例子

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

          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
          主站蜘蛛池模板: 濮阳市| 苏尼特左旗| 宾川县| 廊坊市| 仪陇县| 屏南县| 大城县| 当阳市| 吴堡县| 蒙阴县| 五指山市| 凤凰县| 邓州市| 工布江达县| 崇明县| 海宁市| 平泉县| 青海省| 奉新县| 扎鲁特旗| 铁岭县| 申扎县| 百色市| 东平县| 巩留县| 元阳县| 勃利县| 昭觉县| 怀柔区| 沾化县| 太白县| 车险| 资溪县| 封丘县| 南郑县| 临洮县| 凉山| 赤峰市| 二连浩特市| 扬州市| 杭州市|