欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring rmi

发布时间:2025/3/21 javascript 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring rmi 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

1.org.springframework.remoting.rmi.RmiProxyFactoryBean

其使用的是rmi协议实现

实现过程,首先是服务端

定义一个导出类

?
1 2 3 public  interface  AccountService {      String getUsername(); }

 

?
1 2 3 4 5 6 public  class  AccountServiceImpl  implements  AccountService{      @Override      public  String getUsername() {          return  "RMI Test!" ;      } }

 

rmi.xml

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <? 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.xsd">      < bean  id = "accountService"  class = "example.AccountServiceImpl" >          <!-- any additional properties, maybe a DAO? -->      </ bean >      < bean  class = "org.springframework.remoting.rmi.RmiServiceExporter" >          <!-- 这里的服务名可以随意填写但是必须和rmi://hostname:1199/xxxxxxx的xxxxxxx相同 -->          < property  name = "serviceName"  value = "AccountService" />          <!-- 导出实体 -->          < property  name = "service"  ref = "accountService" />          <!-- 导出接口,这个为导出接口,注意,客户端包名可以和这里不同,但是为了统一建议做一个coreInterface              包,以便以后维护方便 -->          < property  name = "serviceInterface"  value = "example.AccountService" />          <!-- 端口号,默认为1099,这里注意占用问题 -->          < property  name = "registryPort"  value = "1199" />      </ bean > </ beans >

 

启动服务

?
1 2 3 4 5 6 7 8 9 public  class  Main {      public  static  void  main(String[] args) {          ApplicationContext context =                  new  ClassPathXmlApplicationContext( "rmi.xml" );          AccountService service = context.getBean( "accountService" , AccountService. class );          String userName = service.getUsername();          System.out.println(userName);      } }

 

 

接下来客户端如下

?
1 2 3 public  interface  AccountService {      String getUsername(); }

 

rmi.xml

?
1 2 3 4 5 6 7 8 9 10 11 12 13 <?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.xsd">      <bean id= "accountService"  class = "org.springframework.remoting.rmi.RmiProxyFactoryBean" >          <!-- 接收的rmi协议 -->          <property name= "serviceUrl"  value= "rmi://localhost:1199/AccountService" />          <!-- 接收的rmi协议的接口 -->          <property name= "serviceInterface"  value= "example.AccountService" />      </bean> </beans>

 

启动程序

?
1 2 3 4 5 6 7 8 9 public  class  Main {      public  static  void  main(String[] args) {          ApplicationContext context =                  new  ClassPathXmlApplicationContext( "rmi.xml" );          AccountService service = context.getBean( "accountService" , AccountService. class );          String userName = service.getUsername();          System.out.println(userName);      } }

 

这样就可以在服务器端得到了RMI Test!

当我们在启动服务端的时候会发现,其控制台一直在运行状态,当结束后,还会有rmi进程在运行。其接口协议为rmi://hostname:1199/xxxxxxx

 

转载于:https://my.oschina.net/u/264186/blog/638117

总结

以上是生活随笔为你收集整理的Spring rmi的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。