cxf与spring的整合

来源:互联网 发布:mac免费软件推荐 编辑:程序博客网 时间:2024/06/05 19:52

    cxf与spring的整合,这里分两部分,分别为server端与client端

server端

    1.导入jar包

下载apache-cxf,将其lib目录下的jar导入到项目中的lib中

    2.配置web.xml

<!-- 配置beans.xml -->  <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:beans.xml</param-value>   </context-param>      <!-- 应用启动的一个监听器    -->   <listener>      <listener-class>         org.springframework.web.context.ContextLoaderListener      </listener-class>   </listener>      <!--  所有请求都会先经过cxf框架   -->   <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>

    3.创建spring的配置文件beans.xml,在其中配置SEI

<?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.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">    <!-- 引入cxf的一些核心配置 -->   <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" />    <!-- 配置终端:id-名字,implementor-实现者,address-虚拟地址 -->   <jaxws:endpoint      id="orderWS"      implementor="com.atguigu.day02_ws_cxf_spring.ws.OrderWSImpl"      address="/orderws">    </jaxws:endpoint></beans>
4.编写服务端接口和实现类

@WebServicepublic interface OrderWS {@WebMethodpublic Order getOrderById(int id);}
@WebServicepublic class OrderWSImpl implements OrderWS {public OrderWSImpl(){System.out.println("OrderWSImpl()");}public Order getOrderById(int id) {System.out.println("server被调用了,id="+id);return new Order(id, "你好", 1000000);}}

client

    1.生成客户端代码

      如何生成客户端代码在上一篇已经讲到

    2.创建客户端的spring配置文件beans-client.xml

<jaxws:client id="orderClient" serviceClass= "com.atguigu.day02_ws_cxf_spring.ws.OrderWS" address= "http://localhost:8080/day02_ws_cxf_spring/orderws"></jaxws:client>
3.测试

public class ClientTest {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"client-beans.xml"});OrderWS orderWS = (OrderWS) context.getBean("orderClient");Order order = orderWS.getOrderById(24);System.out.println(order);}}
结果为:

1 0