天天看点

【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又非常适合。

本文出自 “” 博客,请务必保留此出处

继续阅读