?通过前面两节的讲解,相信你对CXF框架开始有一些认识了。在当今项目开发中,Spring框架基上都用到过,那么它怎么与CXF结合呢,这就是我们这一间要讲的内容。好了,闲话少说。?
??? 首先,在前面基础上再导入几个spring要用到的几个.jar包:
???????????????1、spring-asm.jar
???????????????2、spring-beans.jar
???????????????3、spring-context.jar
???????????????4、spring-core.jar
???????????????5、spring-expression.jar
???????????????6、spring-aop.jar
???????????????7、spring-web.jar
第一步:新建一个服务端web project,导入要用到的cxf和spring的.jar包,修改web.xml。配置如下
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <web-app?version="2.4"???
- ????xmlns="http://java.sun.com/xml/ns/j2ee"???
- ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
- ????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee???
- ????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">??
- ??????
- ????context-param ????????param-name>contextConfigLocation</>??
- ????????param-value ????????????classpath:/applicationContext-server.xml??
- ???? ??????
- ???????????
- ?????????listener ??????????????????listener-class>org.springframework.web.context.ContextLoaderListener ????????? ??????????????????>org.springframework.web.util.IntrospectorCleanupListener<!--?WebServices设置?-->??
- servletservlet-name>CXFServicesservlet-class>org.apache.cxf.transport.servlet.CXFServletload-on-startup>0servlet-mappingurl-pattern>/services/* ??????
- ??welcome-file-listwelcome-file>index.jspweb-app>??
第二步:新建一个接口类和接口实现类
package?com.ms.services;??
- ??
- import?java.util.List;??
- import?javax.jws.WebService;??
- import?com.ms.model.UserInfo;??
- @WebService??
- public?interface?IHelloServices?{??
- ????public?String?sayHello(String?name);??
- ????public?String?sayHelloToAll(List<UserInfo>?users);??
- }??
package?com.ms.services.impl;??
- import?com.ms.services.IHelloServices;??
- ??
- @WebService(endpointInterface="com.ms.services.IHelloServices")??
- class?HelloServicesImpl?implements?IHelloServices?{??
- public?String?sayHello(String?name)?{??
- ????????return?"Hello?"+name+"?.";??
- ????}??
- public?String?sayHelloToAll(List<UserInfo>?users)?{??
- ????????String?hello?=?"hello?";??
- for(UserInfo?user:users){??
- ????????????hello?+=?user.getUserName()+"?,";??
- ????????}??
- ????????hello?+=?"?,everybody.";??
- return?hello;??
- }??
第三步:新建一个spring Bean的xml文件,配置CXF webservices的服务
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-3.0.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"?<!--?services接口配置?-->??
- bean?id="helloServicesBean"?class="com.ms.services.impl.HelloServicesImpl"?<!--?CXF?配置WebServices的服务名及访问地址?-->??
- jaxws:server?id="helloServices"?address="/HelloServices"???
- ????????????serviceClass="com.ms.services.IHelloServices" ????????????jaxws:serviceBean ????????????????ref?bean="helloServicesBean"jaxws:serverbeans第四步:将工程部署到Tomcat中运行,在IE中输入"
http://localhost:8090/CxfServer_Spring/services
",测试服务是否发布成功
第五步:新建一个客户端web project,导入要用到的cxf和spring的.jar包 第六步:将服务端的接口类及JavaBean对象类copy到客户端工程中,且路径要与服务端路径一致 第七步:新建一个spring Bean的xml配置文件,配置CXF webservices的客户端
<!--?Import?apache?CXF?bean?definition?--> ??
- <!--?CXF?webservices?客户端配置?-->??
- jaxws:client?id="helloClient"?serviceClass="com.ms.services.IHelloServices"???
- ????????????address="http://localhost:8090/CxfServer_Spring/services/HelloServices"jaxws:client第八步:新建一个测试类进行测试,代码如下
package?com.test;??
- import?java.util.ArrayList;??
- import?java.util.List;??
- import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
- import?org.springframework.context.ApplicationContext;??
- import?org.springframework.context.support.ClassPathXmlApplicationContext;??
- import?com.ms.model.UserInfo;??
- import?com.ms.services.IHelloServices;??
- class?Client?{??
- static?void?main(String[]?args)?{??
- ????????invokeBySpring();??
- ????}??
- ?????
- ?
- ??
- void?invokeBySpring(){??
- ????????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("applicationContext-client.xml");??
- ????????IHelloServices?helloServices?=?context.getBean("helloClient",IHelloServices.class);??
- ??????????
- ????????List<UserInfo>?users?=?new?ArrayList<UserInfo>();??
- ????????users.add(new?UserInfo("vicky",23));??
- ????????users.add(new?UserInfo("caty",0); background-color:inherit">23));??
- new?UserInfo("ivy",153); background-color:inherit; font-weight:bold">new?UserInfo("kelly",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????????String?helloAll?=?helloServices.sayHelloToAll(users);??
- ??????????
- ????????System.out.println(helloAll);??
- void?invoke(){??
- ??????????
- ????????JaxWsProxyFactoryBean?factory?=?new?JaxWsProxyFactoryBean();?????
- //注册WebService接口?????
- ????????factory.setServiceClass(IHelloServices.class);?????
- //设置WebService地址?????
- ????????factory.setAddress("http://localhost:8090/CxfServer_Spring/services/HelloServices");??????????
- ????????IHelloServices?helloServices?=?(IHelloServices)factory.create();?????
- ????????System.out.println("invoke?helloServices?webservice...");??
- ????????String?hello?=?helloServices.sayHello("vicky");??
- ????????System.out.println(hello);??
- }??
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|