今天用spring的rmi,启动似乎不注册rmi服务。最后手工的加载bean,成了。
晕,在配置头的时候设置default-lazy-init="true",延迟加载的说。
顺便贴代码
服务器端配置文件
<!--
服务
-->
< bean id ="myService" class ="org.spring.rmi.yoara.MyServiceImpl" />
<!-- RmiServiceExporter 此类封装了生成stub的方法,大大简化代码 -->
< bean id ="serviceExporter" class ="org.springframework.remoting.rmi.RmiServiceExporter" >
<!-- 服务 -->
< property name ="service" ref ="myService" />
<!-- 服务名 -->
< property name ="serviceName" value ="MyService" />
<!-- 绑定服务的路径 -->
< property name ="serviceInterface" value ="org.spring.rmi.yoara.MyService" />
<!-- 服务端口 -->
< property name ="registryPort" value ="1199" />
< bean id ="myService" class ="org.spring.rmi.yoara.MyServiceImpl" />
<!-- RmiServiceExporter 此类封装了生成stub的方法,大大简化代码 -->
< bean id ="serviceExporter" class ="org.springframework.remoting.rmi.RmiServiceExporter" >
<!-- 服务 -->
< property name ="service" ref ="myService" />
<!-- 服务名 -->
< property name ="serviceName" value ="MyService" />
<!-- 绑定服务的路径 -->
< property name ="serviceInterface" value ="org.spring.rmi.yoara.MyService" />
<!-- 服务端口 -->
< property name ="registryPort" value ="1199" />
</bean>
服务pojo随便写就成了。
客户端配置文件
<
bean
id
="goService"
class
="org.springframework.remoting.rmi.RmiProxyFactoryBean"
>
<!-- 服务URL -->
< property name ="serviceUrl" value ="rmi://127.0.0.1:1199/MyService" />
<!-- 本地接口路径 -->
< property name ="serviceInterface" value ="org.spring.rmi.yoara.MyService" />
<!-- 服务URL -->
< property name ="serviceUrl" value ="rmi://127.0.0.1:1199/MyService" />
<!-- 本地接口路径 -->
< property name ="serviceInterface" value ="org.spring.rmi.yoara.MyService" />
</bean>
客户端调用代码
测试连接:
// try {
// System.out.println(Naming.lookup("rmi://localhost:1199/MyService"));
// } catch (Exception e) {
// e.printStackTrace();
}
第一种:
ClassPathXmlApplicationContext cxc = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MyService client = (MyService)cxc.getBean("goService");
client.showInfo();
第二种:
// RmiProxyFactoryBean proxy=new RmiProxyFactoryBean();
// proxy.setServiceInterface(MyService.class);
// proxy.setServiceUrl("rmi://localhost:1199/MyService");
// proxy.afterPropertiesSet();
// MyService client=(MyService)proxy.getObject();
// try {
// System.out.println(Naming.lookup("rmi://localhost:1199/MyService"));
// } catch (Exception e) {
// e.printStackTrace();
}
第一种:
ClassPathXmlApplicationContext cxc = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MyService client = (MyService)cxc.getBean("goService");
client.showInfo();
第二种:
// RmiProxyFactoryBean proxy=new RmiProxyFactoryBean();
// proxy.setServiceInterface(MyService.class);
// proxy.setServiceUrl("rmi://localhost:1199/MyService");
// proxy.afterPropertiesSet();
// MyService client=(MyService)proxy.getObject();
// client.showInfo();