使用spring配置RMI
2013-05-18 20:28 阅读(191)

服务器端:
1.创建一个javabean对象,需要实现Serializable接口。(如果只是传输字符串可以不用创建javabean对象)

import java.io.Serializable;  
public class User implements Serializable {  
    private static final long serialVersionUID = 1L;  
    private String name;  
    private int age;
    ……
}
2.创建一个接口

import com.my.po.User;  
public interface IBaseService {  
    /** 
     * 简单Hello Word 
     * @param name   
     * @return 
     */  
    public String getHelloWord(String name);  
    /** 
     * 获得User对象 
     * @param user 
     * @return 
     */  
    public String getUser(User user);  
}
3.实现类

import com.my.po.User;  
import com.my.service.IBaseService;  
public class BaseServiceImpl implements IBaseService {  
    public String getHelloWord(String name) {  
        return "欢迎"+name+"的到来!!!";  
    }  
    public String getUser(User user) {  
        return "名字:"+user.getName()+"--->"+"年龄:"+user.getAge();  
    }  
}
4.创建一个spring配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    <bean id="baseRmiService" class="com.my.service.impl.BaseServiceImpl" /> 
    <bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  
        <!-- 调用Service -->  
        <property name="service" ref="baseRmiService" />
        <!-- value值是提供给客户端调用 -->  
        <property name="serviceName" value="baseService" />
        <!-- service接口 -->  
        <property name="serviceInterface" value="com.my.service.IBaseService" />
        <!-- 注册端口 -->  
        <property name="registryPort" value="1200" />
    </bean>  
</beans>
5.main方法

import org.springframework.context.support.ClassPathXmlApplicationContext;  
import com.my.service.IBaseService;  
public class BaseServiceTest {   
    public static void main(String[] args) {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
        IBaseService ibs = (IBaseService) context.getBean("baseRmiService");  
        System.out.println("baseRmiService启动...");  
    }  
}

将User.java和IBaseService.java打成jar包,放到客户端里面。

客户端: 
1.创建一个spring配置文件 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"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    <bean id="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
        <!-- baseService是调用服务端serviceName的value,1200是服务端注册的端口 -->  
        <property name="serviceUrl" value="rmi://localhost:1200/baseService" />
        <!-- service接口 -->  
        <property name="serviceInterface" value="com.my.service.IBaseService" />
    </bean>  
</beans>
2.main方法
import org.springframework.context.support.ClassPathXmlApplicationContext;  
import com.my.po.User;  
public class ClientTest {  
    public static void main(String[] args) {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
        IBaseService ibs = (IBaseService) context.getBean("baseService");  
        System.out.println(ibs.getHelloWord("hao"));  
        User user = new User();  
        user.setName("chenghao");  
        user.setAge(24);  
        System.out.println(ibs.getUser(user));  
    }  
}

现在运行ClientTest类就会在控制台打印出:

欢迎hao的到来!!! 名字:chenghao--->年龄:24


来自:开源中国

作者:chenghao

链接:http://my.oschina.net/hao0610/blog/131686