1. 程式人生 > >WebService學習之旅(三)JAX-WS與Spring整合釋出WebService

WebService學習之旅(三)JAX-WS與Spring整合釋出WebService

Spring本身就提供了對JAX-WS的支援,有興趣的讀者可以研究下Spring的Spring-WS專案,專案地址:
http://docs.spring.io/spring-ws/sites/1.5/downloads/releases.html
基於Spring IoC容器釋出Web服務,能夠大大降低WebService實現過程,也能夠更好的與企業級應用進行整合,本文將和大家介紹如何基於Spring和JAX-WS釋出WebService。

我們首先需要獲取專案所依賴的Jar包,這個過程比較繁瑣,筆者採用Maven構建專案,使用Maven進行專案管理的好處是我們只需要在pom.xml檔案中配置依賴專案座標,Maven就會自動將所需要的Jar包下載到本地倉庫。

1.新建一個Maven Web專案,在pom.xml中新增如下內容:

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
<groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId
>
<version>1.1.6</version> </dependency>

2.新建Web服務介面和實現類,這個過程和前面兩篇文中相同。
HelloWorld.java

package com.csdn.ws.recipe03;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
    @WebMethod
    public String sayHello(String name);
}

HelloWorldImpl.java

package com.csdn.ws.recipe03;

import javax.jws.WebService;
import org.springframework.stereotype.Component;

@Component
@WebService(serviceName = "HelloWorldService", endpointInterface = "com.csdn.ws.recipe03.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHello(String name) {
        return "Hello," + name;
    }
}

不同的是在實現類中添加了註解@Component,該註解用於Spring查詢元件。

3.在web.xml檔案中新增spring的監聽器配置:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

4.新建一個source folder,名為config,在config下新建beans.xml,用於spring bean的配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.csdn.ws.recipe03"></context:component-scan>
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
        <property name="baseAddress" value="http://localhost:8089/services/"/>
    </bean>

</beans>

context:component-scan標籤指定了查詢元件的包名,SimpleJaxWsServiceExporter類的baseAddress屬性用於指定webservice的根路徑,完整的web服務地址=根路徑+WebService名稱。
5.完整的專案結構如下圖所示:

這裡寫圖片描述

7.WebService客戶端呼叫程式碼請參考WebService學習之旅系列第一篇文章。

下節開始介紹開源WebService框架Apache Axis2的使用。