Using Spring And CXF:
http://www.ibm.com/developerworks/webservices/library/ws-pojo-springcxf/index.html?ca=drs-
在我们下载的CXF distribution包中的samples下也提供了一个示例:java_first_spring_support
新建一个javaweb项目,为了方便这里引入CXF lib下的所有jar包,CXF内置了Spring的支持,我们也可以从官网上下载Spring jar包

定义一个接口:
@WebService
public interface CXFSpringHelloWorld {
@WebMethod
public String sayHelloWithCXFSpring(@WebParam(name="name") String name);
}
实现类:
@WebService(endpointInterface="cn.nevo.cxf.spring.ws.CXFSpringHelloWorld")
public class CXFSpringHelloWorldImpl implements CXFSpringHelloWorld {
public String sayHelloWithCXFSpring(String name) {
return name + ", The CXF with Spring!";
}
}
Spring配置文件 spring-cxf.xml:
在Spring的配置文件中引入CXF的XML Scheam配置
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
另外还需要引入以下3个关于CXF的资源文件,这三个文件在cxf.jar中
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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://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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="hello" class="cn.nevo.cxf.spring.ws.impl.CXFSpringHelloWorldImpl" />
<!-- address为webservice发布的地址 -->
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/CXFSpringHelloWorld" />
<!-- implementorClass 为该webservice实现的接口
<jaxws:implementor>之间为指定接口的实现类
-->
<!--
<jaxws:endpoint id="hello" implementorClass="cn.nevo.cxf.spring.ws.CXFSpringHelloWorld"
address="/helloworld">
<jaxws:implementor>
<bean id="cxfSpringHelloWorldImpl"
class="cn.nevo.cxf.spring.ws.impl.CXFSpringHelloWorldImpl"/>
</jaxws:implementor>
</jaxws:endpoint>
-->
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-cxf.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>/*</url-pattern>
</servlet-mapping>
</web-app>
启动服务器,http://localhost:8080/cxf_with_spring/ CXFSpringHelloWorld?wsdl访问结果:
CXFSpringHelloWorld为<jaxws:endpoint >标签中address属性对应的值
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.ws.spring.cxf.nevo.cn/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://ws.spring.cxf.nevo.cn/" name="CXFSpringHelloWorldImplService" targetNamespace="http://impl.ws.spring.cxf.nevo.cn/">
<wsdl:import location="http://localhost:8080/cxf_with_spring/CXFSpringHelloWorld?wsdl=CXFSpringHelloWorld.wsdl" namespace="http://ws.spring.cxf.nevo.cn/" />
- <wsdl:binding name="CXFSpringHelloWorldImplServiceSoapBinding" type="ns1:CXFSpringHelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHelloWithCXFSpring">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHelloWithCXFSpring">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloWithCXFSpringResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="CXFSpringHelloWorldImplService">
- <wsdl:port binding="tns:CXFSpringHelloWorldImplServiceSoapBinding" name="CXFSpringHelloWorldImplPort">
<soap:address location="http://localhost:8080/cxf_with_spring/CXFSpringHelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端调用(Spring环境)
客户端配置文件spring-cxf-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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://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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:client id="cxfSpringClient"
serviceClass="cn.nevo.cxf.spring.ws.CXFSpringHelloWorld"
address="http://localhost:8080/cxf_with_spring/CXFSpringHelloWorld"
/>
</beans>
CXFSpringClient.java
public class CXFSpringClient {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-cxf-client.xml");
CXFSpringHelloWorld csh =
(CXFSpringHelloWorld)context.getBean("cxfSpringClient");
System.out.println(csh.sayHelloWithCXFSpring("China"));
}
}
输出:
China, The CXF with Spring!