xfire與spring集成

近期有一個項目,採用xfire作爲webserice框架提供服務,由於需要更改數據庫訪問框架,要將xfire與spring框架集成,經過實踐,集成步驟如下:

(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完成。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章