近期專案需要使用 Web Service,經過研究 Spring 整合 CXF 的官方文檔(文檔說明清晰,建議直接閱讀官方英文文檔),整理出一份完整的 Demo 供參考。本教程採用 Maven + Spring + CXF + IntelliJ IDEA 技術棧,帶你從零搭建一個可運行的 Web Service 應用。
專案採用標準 Maven 目錄結構,核心模塊分為服務端與客戶端兩部分。以下為專案結構圖:

首先創建 HelloWorld 接口,使用 @WebService 標註為 Web Service 端點:
package cn.starlin.springcxf;
import javax.jws.WebService;
/**
* 定義服務接口
*/
@WebService
public interface HelloWorld {
String sayHello(String str);
}
創建實現類 HelloWorldImpl,通過 endpointInterface 指定對應接口:
package cn.starlin.springcxf;
import javax.jws.WebService;
/**
* 服務接口實現類
*/
@WebService(endpointInterface = "cn.starlin.springcxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String str) {
System.out.println("sayHello called");
return "Hello " + str;
}
}
在 resources 目錄下創建 cxf-servlet.xml,聲明 Service Bean 並發布端點:
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="https://cxf.apache.org/jaxws"
xsi:schemaLocation="
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://cxf.apache.org/jaxws https://cxf.apache.org/schemas/jaxws.xsd">
<!-- 由 Spring 管理服務實現 Bean -->
<bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" />
<!-- 發布 Web Service 端點,地址為 /HelloWorld -->
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>
<web-app xmlns="https://java.sun.com/xml/ns/javaee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
啟動 Tomcat,訪問 https://localhost:8080/SpringCXF/,若出現 CXF 服務列表頁面,表示服務端發布成功:

創建 client-beans.xml,使用 JaxWsProxyFactoryBean 創建代理:
<?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:jaxws="https://cxf.apache.org/jaxws"
xsi:schemaLocation="
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://cxf.apache.org/jaxws https://cxf.apache.org/schema/jaxws.xsd">
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/>
<property name="address" value="https://localhost:8080/SpringCXF/HelloWorld"/>
</bean>
<bean id="client" class="cn.starlin.springcxf.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
</beans>
package cn.starlin.springcxf;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 客戶端測試類
*/
public class HelloWorldClient {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
HelloWorld client = (HelloWorld) context.getBean("client");
String response = client.sayHello("starlin");
System.out.println("Response: " + response);
System.exit(0);
}
}
運行 HelloWorldClient,控制台輸出 Response: Hello starlin 即表示調用成功。服務端控制台同步打印 sayHello called。


至此,Spring 整合 CXF 的 Web Service 開發全流程完成。掌握此模式,可快速構建企業級 SOAP 服務。