程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring RMI Example,springrmiexample

Spring RMI Example,springrmiexample

編輯:JAVA綜合教程

Spring RMI Example,springrmiexample


一: 提供服務的遠程一端

1-1. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="userRmiServiceImpl" class="com.goodfan.rmi.service.impl.UserRmiServiceImpl" />
    <bean id="userRmi" class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="service" ref="userRmiServiceImpl" />
        <property name="serviceName" value="userRmi" />
        <property name="serviceInterface" value="com.goodfan.rmi.service.UserRmiService" />
        <property name="registryPort" value="9999" />
    </bean>
</beans>  

1-2. 接口

package com.goodfan.rmi.service;

public interface UserRmiService {
     public String sayHello(User user);
}

1-3. javabean

package com.goodfan.rmi.service;

import java.io.Serializable;  

public class User implements Serializable{  
  
    private static final long serialVersionUID = 8550373205815267923L;  
    private String userName;  
  
    public String getUserName() {  
        return userName;  
    }  
  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
  
} 

1-4. 實現類

package com.goodfan.rmi.service.impl;

import com.goodfan.rmi.service.User;
import com.goodfan.rmi.service.UserRmiService;

public class UserRmiServiceImpl implements UserRmiService {

    @Override
    public String sayHello(User user) {
         return "Hello, " + user.getUserName();
    }

}

1-5. ServerTest類

package com.goodfan.rmi.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;  

public class ServerTest {  
  
    public static void main(String[] args) {  
        System.setProperty("java.rmi.hostname", "10.7.3.12");   
        new ClassPathXmlApplicationContext("applicationContext.xml");  
        System.out.println("server start......");  
    }  
}

 

二: 本地調用一端

2-1. applicationContext-client

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
    <bean id="rmiProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
        <property name="serviceUrl" value="rmi://10.7.3.12:9999/userRmi"/>  
        <property name="ServiceInterface" value="com.goodfan.rmi.service.UserRmiService" />  
    </bean>  
</beans> c

2-2. ClientTest類

package com.goodfan.rmi.service;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class ClientTest {  
  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
        UserRmiService ums = (UserRmiService) ctx.getBean("rmiProxy");  
        User user = new User();  
        user.setUserName("RMI");  
        System.out.println(ums.sayHello(user));  
    }  
} 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved