首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Axis2 系列教程(七):利用 Spring 整合發布 JavaBean 為 Web Service

     編輯:Java 相關知識

前言

在現代 Web 應用開發中,Spring 框架已成為管理 JavaBean 依賴關係的主流選擇。若要將 Spring 容器中裝配好的 Bean 直接發布為 Web Service,Axis2 內建的 Spring 整合機制(Spring-aware)提供了極為便捷的路徑。本文將逐步示範如何在 Axis2 環境下,透過簡單的設定將 Spring Bean 暴露為 Web Service。

環境準備

除了 Axis2 基本安裝目錄(<Tomcat安裝目錄>\webapps\axis2)及其相關庫外,還需將 Spring 框架的核心 spring.jar 複製到 <Tomcat安裝目錄>\webapps\axis2\WEB-INF\lib 目錄中。

建立待發布的 JavaBean

首先定義一個簡單的服務類 SpringService,並包含一個內部使用的 Person 實體類:

SpringService.java

package service;
import entity.Person;

public class SpringService {
    private String name;
    private String job;

    public void setName(String name) { this.name = name; }
    public void setJob(String job) { this.job = job; }

    public Person getPerson() {
        Person person = new Person();
        person.setName(name);
        person.setJob(job);
        return person;
    }

    public String getGreeting(String name) {
        return "hello " + name;
    }
}

Person.java

package entity;

public class Person {
    private String name;
    private String job;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getJob() { return job; }
    public void setJob(String job) { this.job = job; }
}

將上述兩個類編譯後,將 .class 檔案置於 <Tomcat安裝目錄>\webapps\axis2\WEB-INF\classes 目錄下(保持套件結構)。

配置 Spring 容器

修改 web.xml

在 web.xml 中註冊 Spring 的 ContextLoaderListener 並指定配置檔位置:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

編寫 applicationContext.xml

在 WEB-INF 目錄建立 applicationContext.xml,定義 springService Bean 並注入屬性值:

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

    <bean id="springService" class="service.SpringService">
        <property name="name" value="姚明" />
        <property name="job" value="職業男籃" />
    </bean>
</beans>

發布為 Web Service

Axis2 提供 axis2-spring-1.4.1.jar(位於 WEB-INF/lib)以支援 Spring Bean 的直接發布。建立一個 AAR 封裝檔:

  1. 在任意目錄(如 D:\axi2-spring-ws)建立 META-INF 子目錄。
  2. 在 META-INF 下建立 services.xml:
<service name="springService">
    <description> Spring aware </description>
    <parameter name="ServiceObjectSupplier">
        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
    </parameter>
    <parameter name="SpringBeanName"> springService </parameter>
    <messageReceivers>
        <messageReceiver mep="https://www.w3.org/2004/08/wsdl/in-out"
                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>
</service>
  1. 在命令列進入 axi2-spring-ws 目錄,執行 jar cvf axi2-spring-ws.aar . 打包。
  2. 將生成的 axi2-spring-ws.aar 複製到 <Tomcat安裝目錄>\webapps\axis2\WEB-INF\services 目錄。
  3. 啟動 Tomcat,即可透過 https://localhost:8080/axis2/services/springService?wsdl 獲取 WSDL。

關鍵注意事項

  • 類檔案放置:Bean 的 .class 檔必須置於 WEB-INF/classes 或打包為 JAR 置於 WEB-INF/lib;AAR 內僅需包含 META-INF/services.xml,不可包含類檔案。
  • services.xml 配置:不使用 ServiceClass 參數,改用 SpringBeanName 指向 Spring 容器中的 Bean ID;並透過 ServiceObjectSupplier 參數指定 SpringServletContextObjectSupplier 以獲取 Spring ApplicationContext。

完成上述步驟後,Spring 管理的 Bean 即可無縫暴露為 Web Service,兼具依賴注入與服務發布的雙重優勢。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved