當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring rmi
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven 工程启动找不到 Spring
- 下一篇: gradle idea java ssm