Spring RMI Example

一: 提供服务的远程一端

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));  
    }  
} 

 

posted @ 2016-05-17 19:43  fangfan  阅读(362)  评论(0编辑  收藏  举报