千家信息网

RMI spring

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,1.业务接口类及其实现/** * 定义一个远程接口 */public interface HelloService{ /** * 需要远程调用的方法 * @param msg
千家信息网最后更新 2025年12月02日RMI spring

1.业务接口类及其实现

/** * 定义一个远程接口 */public interface HelloService{    /**     * 需要远程调用的方法     * @param msg     * @return     */    String sayHello(String msg);}


public class HelloServiceImpl implements HelloService{    public String sayHello(String msg)    {        return "server received the msg : " + msg;    }}


2.RmiServiceExporter(服务端)

                                                                                                                


3.RmiProxyFactoryBean(客户端)

                        


4.测试

public class HelloServer{    public static void main(String[] args)    {        new ClassPathXmlApplicationContext("spring-rmi-server.xml");        System.err.println("rmi 服务启动!");    }}
public class HelloClient{    public static void main(String[] args)    {        ApplicationContext applicationContext         = new ClassPathXmlApplicationContext("spring-rmi-client.xml");        HelloService helloService         = (HelloService)applicationContext.getBean("helloService");        System.err.println(helloService.sayHello("测试测试!"));    }}


注:Registry服务的注册问题,有时会出现服务启动报错.


0