天天看點

【Spring】幾種RPC模型的使用與比較——JAX-WS

看來使用web service是不可避免的。

我曾對這個有些抵觸,因為他給我印象總是麻煩+慢(後來雖然友善了許多,但還是很慢)。

然後再去搜尋"advantages of web service"什麼的試着再讓自己接受他。

簡單記錄一下如何用spring導出endpoint。

假設我想在有一個spring應用,我需要把一個pojo或者一部分方法導出為web service。

但這會有一個問題——endpoint的生命周期是由jax-ws runtime來管理(the lifecycle of such an endpoint instance will be managed by the jax-ws runtime),spring context中的bean無法autowire到endpoint中,而我要導出的那些東東都用到了spring管理的bean。

對此,我們有兩個解決方法:

·org.springframework.web.context.support.springbeanautowiringsupport

·jaxwsserviceexporter

我上面括号中的那段話是引用的springbeanautowiringsupport的javadoc。

使用該類的典型案例就是bean注入到jax-ws endpoint類中(人家注釋上寫的),任何一個生命周期不是由spring來管理的場景都可以用到他。

而我們隻需要繼承這個類,也就是說建立一個執行個體時會調用父類的無參構造方法,我們來看看他的構造方法:

那就試試看:

接着釋出一下:

調用:

寫一個endpoint還要繼承和業務無關的類,讓人不爽...而且釋出和調用都麻煩。

那試試simplejaxwsserviceexporter,隻需要簡單的配置就可以導出一個endpoint。

但是他也有需要注意的地方,引用一下該類的javadoc:

note that this exporter will only work if the jax-ws runtime actually supports publishing with an address argument, i.e. if the jax-ws runtime ships an internal http server. this is the case with the jax-ws runtime that‘s inclued in sun‘s jdk 1.6 but not with the standalone jax-ws 2.1 ri.

simplejaxwsserviceexporter會自動detect所有被webservice注解的類,是以隻需要在配置中聲明即可;此時endpoint位址直接使用預設的localhost:8080:

接着改善一下用戶端的調用,使用jaxwsportproxyfactorybean:

這樣就可以像使用普通bean一樣使用service了:

用起來友善了不少,但仍然無法改變一個事實:

web service requests are larger than requests encoded with a binary protocol.

還有就是http/https的問題。

如果使用不當,我可能需要多做些工作去處理http做不來的事情,而這時候rmi又非常适合。

本文出自 “” 部落格,請務必保留此出處

繼續閱讀