Apache CXF With Spring

本文详细介绍如何使用Apache CXF框架与Spring框架进行整合,包括搭建环境、定义接口及实现类、配置Spring XML文件和web.xml等步骤。通过具体实例演示了如何发布和调用Web服务。
官方文档:http://cxf.apache.org/docs/writing-a-service-with-spring.html
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的配置文件中引入CXFXML 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!

转载于:https://my.oschina.net/xiaomaoandhong/blog/117456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值