近期有一个项目,采用xfire作为webserice框架提供服务,由于需要更改数据库访问框架,要将xfire与spring框架集成,经过实践,集成步骤如下:
(3)实现接口:
其中serviceName是服务的名字,可以自行定义,endpointInterface是实现的接口类。
在此配置中采用了注解的方式来发布服务,对应接口实现类中使用注解的类,可以发布多个wsdl文档,只需要添加相对应的使用注解配置的实现类即可.
(1)web.xml配置更改,拦截xfire请求:
<!-- begin Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- end Spring配置 -->
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!-- 在这个URI下开放Web Service服务 -->
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
(2)建立webserice接口,使用@WebService注解:
@WebService
public interface IHealthButler {
public String getHealthButler(String xmlStr)throws Exception;
}
(3)实现接口:
@WebService(serviceName="JKDGJINTERFACE02",endpointInterface="com.yiyamh.webservice.inter.IHealthButler")
public class HealthButlerImple implements IHealthButler {
public String getHealthButler(String xmlStr) throws Exception () {
return "hello world";
}
}
其中serviceName是服务的名字,可以自行定义,endpointInterface是实现的接口类。
(3)spring配置文件applicationContext.xml更改:
<!--<bean id="HelloWorldBean" class="com.yiyamh.ws.HelloWorldImpl"/> -->
<!-- 要发布成web服务的pojo,可以添加多个,对外发布多个WSDL -->
<bean id="serviceBean" class="com.yiyamh.webservice.imple.HealthButlerImple"/>
<!--<bean id="serviceBean2" class="com.yiyamh.service.laikon.impl.LaiKonServiceImpl"/>-->
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 获得applicationContext中所有bean的JSR181 annotation -->
<bean id="webAnnotations"
class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
<bean id="jsr181HandlerMapping"
class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="xfire" ref="xfire" />
<property name="webAnnotations" ref="webAnnotations" />
</bean>
在此配置中采用了注解的方式来发布服务,对应接口实现类中使用注解的类,可以发布多个wsdl文档,只需要添加相对应的使用注解配置的实现类即可.
(4)启动服务,访问wsdl文档:
至此集成spring完成。