spring3.0 与webservice结合

如果只说webservice的搭建的话,用myeclipse+xfire就可以轻松搞定,网上的教程多如牛毛。但是前两天由于项目需要,要用到spring+webservice结合,出现了不少问题花了小一天时间才搞定,下面总结一下:

1、只配置webservice的话,用xfire配置就可以搞定。

2、要用到spring的话,就不一样了,因为要用spring的自动注入或是annotation,一句说就是用spring来配置xfire从而得到webservice的效果。

具体步骤是:

   (1)    在web.xml中拦截webservice的请求,配置如下:

            <!-- webservices响应的配置 -->
 <!-- webservices响应的配置 -->
 <servlet>
     <servlet-name>xfire</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!--此注释是xfire的拦截配置,上边是spring配置xfire的参数

<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
      -->
     <load-on-startup>0</load-on-startup>
   </servlet>
  <servlet-mapping>
     <servlet-name>xfire</servlet-name>
     <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

      (2) 在eclipse的项目中的WEB-INF下新建一个xfire-servlet.xml文件,这个文件的名字要和上边列出的配置参数的<servlet-name>xfire</servlet-name>中的值,即:xfire,此文件是为了配置webservice的各个服务类及其函数准备的。

       ( 3 )在web.xml中的classpath参数配置中,加入xfire-servlet.xml的路径, 如下:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>//","用于加入多个classpath变量。
          classpath*:/applicationContext*.xml,
          classpath*:/xfire-servlet.xml
  </param-value>
 </context-param>

        ( 4 ) 写入xfire-servlet.xml中的具体参数内容,

//头已省略,以下为具体的配置。

   <description>webservice的配置</description>

//此下的import必须要加入,是为其后的参数做准备的。
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
    //此处的name,即为请求时的名称,
 <bean name="/GroupingService" class="org.codehaus.xfire.spring.remoting.XFireExporter">

//这里的serviceInterface是和xfire配置时一致的,也就是加个接口。
  <property name="serviceInterface" value="example.IGroupingService" />

//这里的serviceInterface是和xfire配置时一致的,也就是加个这个接口的实现类。
  <property name="serviceBean">
   <bean class="example.GroupingServiceImpl" />
  </property>      

   <!-- the XFire bean is defined in the xfire.xml file -->
  <property name="xfire" ref="xfire" />
 </bean>

</beans>

    (5)此时就算配置完成了,这时的webservice用到的服务类中,就可以很顺利的用spring的

annotation及其它注入参数。

       至于测试的类及函数写法,和xfire配置webservice是一样的,

       试试看,有问题欢迎交流。

你可能感兴趣的:(java,spring,webservice)