在現代 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 目錄中。
首先定義一個簡單的服務類 SpringService,並包含一個內部使用的 Person 實體類:
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;
}
}
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 目錄下(保持套件結構)。
在 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>
在 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>
Axis2 提供 axis2-spring-1.4.1.jar(位於 WEB-INF/lib)以支援 Spring Bean 的直接發布。建立一個 AAR 封裝檔:
D:\axi2-spring-ws)建立 META-INF 子目錄。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>
axi2-spring-ws 目錄,執行 jar cvf axi2-spring-ws.aar . 打包。axi2-spring-ws.aar 複製到 <Tomcat安裝目錄>\webapps\axis2\WEB-INF\services 目錄。https://localhost:8080/axis2/services/springService?wsdl 獲取 WSDL。.class 檔必須置於 WEB-INF/classes 或打包為 JAR 置於 WEB-INF/lib;AAR 內僅需包含 META-INF/services.xml,不可包含類檔案。ServiceClass 參數,改用 SpringBeanName 指向 Spring 容器中的 Bean ID;並透過 ServiceObjectSupplier 參數指定 SpringServletContextObjectSupplier 以獲取 Spring ApplicationContext。完成上述步驟後,Spring 管理的 Bean 即可無縫暴露為 Web Service,兼具依賴注入與服務發布的雙重優勢。