服务端:
1.接口
package com.base.rmi;/** * @Description: * rmi接口 */public interface RMITestInterface { public void testRMI();}
2.实现类
/** * @Description: * RMI实现 */@Service(value="rmiTestProvider")public class RmiTestProvider implements RMITestInterface { @Override public void testRMI() { System.out.println("welcome use RMI!"); }}
3.发布配置
package com.core.config;import com.base.rmi.RMITestInterface;import com.base.rmi.RmiTestProvider;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.remoting.rmi.RmiServiceExporter;/** * @Description: * RMI配置 */@Configurationpublic class RmiConfig { @Autowired @Qualifier("rmiTestProvider") private RmiTestProvider rmiTestProvider; @Bean public RmiServiceExporter rmiServiceExporter() { RmiServiceExporter exporter = new RmiServiceExporter(); exporter.setServiceInterface(RMITestInterface.class); exporter.setServiceName("rmiService"); exporter.setService(rmiTestProvider); exporter.setServicePort(6666); exporter.setRegistryPort(5555); return exporter; }}
客户端:
1.接口
package com.base.rmi;/** * @Description: * rmi接口 */public interface RMITestInterface { public void testRMI();}
2.配置类:
package com.core.config;import com.base.rmi.RMITestInterface;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.remoting.rmi.RmiProxyFactoryBean;/** * @Description: * RMI配置 */@Configurationpublic class RmiConfig { @Bean(name = "rmiService") public RmiProxyFactoryBean rmiProxyFactoryBean(){ RmiProxyFactoryBean rmiProxyFactoryBean=new RmiProxyFactoryBean(); rmiProxyFactoryBean.setServiceInterface(RMITestInterface.class); rmiProxyFactoryBean.setServiceUrl("rmi://localhost:5555/rmiService"); return rmiProxyFactoryBean; }}
3.调用:
(1)注入:
@Autowired@Qualifier("rmiService")private RmiProxyFactoryBean factoryBean;
(2)调用:
/** * 测试RMI * @return */ @ResponseBody @RequestMapping("/testRMI") public String testRMI(){ RmiTestProvider rmiTestProvider=(RmiTestProvider)factoryBean.getObject(); rmiTestProvider.testRMI(); return "RMI调用成功."; }