spring和cxf整合

来源:互联网 发布:mysql自动增长赋值 编辑:程序博客网 时间:2024/06/10 03:44

cxf 是一个开源的服务框架。详细介绍请看官网http://cxf.apache.org/

一、配置所需jar包

整合spring 项目使用maven 进行管理。

pom中cxf部分

<span style="white-space:pre"></span><!-- cxf  --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><!-- Jetty is needed if you're are not using the CXFServlet --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-tools-common</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-tools-java2ws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-tools-validator</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-tools-wsdlto-core</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId><version>${cxf.version}</version></dependency>


spring部分(此部分是手动添加的)




二、配置配置文件web.xml appliationContext.xml

web.xml中添加
<<span style="white-space:pre"></span>ontext-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
<span style="white-space:pre"><servlet><span style="white-space:pre"></span><servlet-name>CXFServlet</servlet-name><span style="white-space:pre"></span><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><span style="white-space:pre"></span><load-on-startup>1</load-on-startup><span style="white-space:pre"></span></servlet><span style="white-space:pre"></span><!-- CXFServlet Mapping --><span style="white-space:pre"></span><servlet-mapping><span style="white-space:pre"></span><servlet-name>CXFServlet</servlet-name><span style="white-space:pre"></span><url-pattern>/services/*</url-pattern><span style="white-space:pre"></span></servlet-mapping></span>

applicationContext.xml
注意标红位置,引用jaxws命名空间
    <span style="white-space:pre"></span><beans xmlns="http://www.springframework.org/schema/beans"<span style="white-space:pre"></span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<span style="white-space:pre"></span>xmlns:p="http://www.springframework.org/schema/p"<span style="white-space:pre"></span>xmlns:jaxws="http://cxf.apache.org/jaxws"</span><span style="white-space:pre"></span>xsi:schemaLocation="<span style="white-space:pre"></span>http://www.springframework.org/schema/beans <span style="white-space:pre"></span>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd<span style="white-space:pre"></span>http://cxf.apache.org/jaxws</span>   <span style="white-space:pre"></span>http://cxf.apache.org/schemas/jaxws.xsd</span>"><span style="white-space:pre"></span><import resource="classpath:META-INF/cxf/cxf.xml"/>  <span style="white-space:pre"></span><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  <span style="white-space:pre"></span><import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <span style="white-space:pre"></span><bean id="hellobServiceImpl" class="test.impl.HelloServiceImpl"><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><jaxws:server id="helloService" serviceClass="test.OrganizationWebService" address="/helloService"><jaxws:serviceBean><ref bean="helloServiceImpl"/></jaxws:serviceBean>    <span style="white-space:pre"></span></jaxws:server>


三、编写服务

接口

实现

写完之后 启动tomcat,浏览器地址输入 http://localhost:8080/test/services,即可看到服务。
wsdl地址是  http://localhost:8080/test/services/helloService?wsdl

四、客户端调用

客户端调用有多种方式,请看官网。本次使用动态调用的方式,具体请看官网。


大功告成!

0 0
原创粉丝点击