當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring rmi
生活随笔
收集整理的這篇文章主要介紹了
Spring rmi
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.org.springframework.remoting.rmi.RmiProxyFactoryBean
其使用的是rmi協(xié)議實(shí)現(xiàn)
實(shí)現(xiàn)過程,首先是服務(wù)端
定義一個(gè)導(dǎo)出類
?| 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" > ???????? <!--?這里的服務(wù)名可以隨意填寫但是必須和rmi://hostname:1199/xxxxxxx的xxxxxxx相同?--> ???????? < property? name = "serviceName"? value = "AccountService" /> ???????? <!--?導(dǎo)出實(shí)體?--> ???????? < property? name = "service"? ref = "accountService" /> ???????? <!--?導(dǎo)出接口,這個(gè)為導(dǎo)出接口,注意,客戶端包名可以和這里不同,但是為了統(tǒng)一建議做一個(gè)coreInterface ???????????? 包,以便以后維護(hù)方便?--> ???????? < property? name = "serviceInterface"? value = "example.AccountService" /> ???????? <!--?端口號(hào),默認(rèn)為1099,這里注意占用問題?--> ???????? < property? name = "registryPort"? value = "1199" /> ???? </ bean > </ beans > |
?
啟動(dòng)服務(wù)
?| 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協(xié)議?--> ???????? <property?name= "serviceUrl"? value= "rmi://localhost:1199/AccountService" /> ???????? <!--?接收的rmi協(xié)議的接口?--> ???????? <property?name= "serviceInterface"? value= "example.AccountService" /> ???? </bean> </beans> |
?
啟動(dòng)程序
?| 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); ???? } } |
?
這樣就可以在服務(wù)器端得到了RMI Test!
當(dāng)我們?cè)趩?dòng)服務(wù)端的時(shí)候會(huì)發(fā)現(xiàn),其控制臺(tái)一直在運(yùn)行狀態(tài),當(dāng)結(jié)束后,還會(huì)有rmi進(jìn)程在運(yùn)行。其接口協(xié)議為rmi://hostname:1199/xxxxxxx
?
轉(zhuǎn)載于:https://my.oschina.net/u/264186/blog/638117
總結(jié)
以上是生活随笔為你收集整理的Spring rmi的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven 工程启动找不到 Spring
- 下一篇: 使用infinite-scroll实现G