Spring 3 整合Apache CXF WebService


在CXF2版本中,整合Spring3發布CXF WebService就更加簡單了。因為Spring 3提供了annotation注解,而CXF2發布WebService已經不像之前版本的配置那樣(參考老版本發布WebService系列文章:http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html),現在發布一個WebService可以直接從Spring的IoC容器中拿到一個對象,發布成WebService服務。當然發布WebService的配置有了些小小的變動,具體請往下看。

 

在老版本中發布一個WebService,配置applicationContext-server.xml文件中添加如下配置如下:

jaxws:server的發布方式

<bean id="userServiceBean" class="com.hoo.service.ComplexUserService"/>
 
<bean id="inMessageInterceptor" class="com.hoo.interceptor.MessageInterceptor">
    <constructor-arg  value="receive"/>
</bean>
 
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,這里的address的名稱就是訪問的WebService的name -->
<jaxws:server id="userService" serviceClass="com.hoo.service.IComplexUserService" address="/Users">
    <jaxws:serviceBean>
        <!-- 要暴露的 bean 的引用 -->
        <ref bean="userServiceBean"/>
    </jaxws:serviceBean>
    <jaxws:inInterceptors>
        <ref bean="inMessageInterceptor"/>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <ref bean="outLoggingInterceptor"/>
    </jaxws:outInterceptors>
</jaxws:server>

jaxws:endpoint的發布方式

<!-- com.hoo.service.ComplexUserService是com.hoo.service.IComplexUserService接口的實現, 這種方法應該不能從Ioc中引用對象 -->
<jaxws:endpoint id="userService2" implementor="com.hoo.service.ComplexUserService" address="/Users">
    <jaxws:inInterceptors>
        <ref bean="inMessageInterceptor"/>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <ref bean="outLoggingInterceptor"/>
    </jaxws:outInterceptors>
</jaxws:endpoint>

而在2.x新版本中,發布Ioc容器中的對象為一個WebService的方法

<bean id="userServiceBean" class="com.hoo.service.ComplexUserService"/>
 
<bean id="inMessageInterceptor" class="com.hoo.interceptor.MessageInterceptor">
    <constructor-arg  value="receive"/>
</bean>
 
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,這里的address的名稱就是訪問的WebService的name;#userServiceBean是直接引用Ioc容器中的Bean對象 -->
<jaxws:server id="userService" serviceBean="#userServiceBean" address="/Users">
    <jaxws:inInterceptors>
        <ref bean="inMessageInterceptor"/>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <ref bean="outLoggingInterceptor"/>
    </jaxws:outInterceptors>
</jaxws:server>
<!-- 或者這種方式,在老版本中這個是不能引用Ioc容器中的對象,但在2.x中可以直接用#id或#name的方式發布服務 -->
<jaxws:endpoint id="userService2" implementor="#userServiceBean" address="/Users">
    <jaxws:inInterceptors>
        <ref bean="inMessageInterceptor"/>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <ref bean="outLoggingInterceptor"/>
    </jaxws:outInterceptors>
</jaxws:endpoint>

CXF發布WebService官方參考:http://cxf.apache.org/docs/writing-a-service-with-spring.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM