1.在pom文件中添加相关依赖:
spring使用的版本为:4.0.0.RELEASE
添加CXF相关依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-common</artifactId>
<version>2.5.4</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
2.编写服务接口
package com.xmg.cxfspring;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface ICalculateService {
@WebMethod(operationName = "add")
@WebResult(name = "result")
public int add(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2);
}
注意:“@WebService”标记表示该接口是一个WebService服务;“@WebMethod”表示表示以下方法为WebService服务中的方法,exclude=true表示该方法不被发布出去.“@WebParam(name="username")”表示方法中的参数,username属性限制了参数的名称,若没有指定该属性,参数将被重命名。
3.编写服务实现类
package com.xmg.cxfspring;
import javax.jws.WebService;
@WebService(endpointInterface = "com.xmg.cxfspring.ICalculateService")
public class CalculateServiceImpl implements ICalculateService {
@Override
public int add(int num1, int num2) {
return num1 + num2;
}
}
注意:@WebService上的endpointInterface写的是该实现类所实现的接口的全限定名
4. 添加WebService声明已引入相关文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml" />
<!-- 配置服务实现类 -->
<bean id="calculateServiceImpl" class="com.xmg.cxfspring.CalculateServiceImpl"></bean>
<!-- 相当于使用JaxWsServerFactoryBean进行服务发布 -->
<jaxws:server id="calculateService" serviceClass="com.xmg.cxfspring.ICalculateService" serviceBean="#calculateServiceImpl" address="/calculateService"></jaxws:server>
<!-- 相当于使用Endpoint.publish()进行服务发布
<jaxws:endpoint address="/calculateService" implementorClass="#calculateServiceImpl"/>
-->
5.在web.xml中配置CXF的Servlet类.
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
注意:如果是使用springmvc启动spring容器的方式,不能使用通过import的方式引入其他spring配置文件.否则cxf的服务启动不了.
需要分开启动如下:
<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>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
具体原因可以看org.apache.cxf.transport.servlet.CXFServlet,发现是去spring的父容器中取找的bean.所以这里需要注意了.
6.启动服务器.启动成功后,打开浏览器,输入 http://localhost/ws/calculateService?wsdl
如果出现如下界面说明CXF和spring集成成功.