cxf与spring的整合

时间:2024-06-11 19:06:56

cxf与spring的整合:

一:服务端相关配置(配置好后启动tomocat就自动发布了接口,浏览器打开验证下)

1:导入cxf以及spring的相关jar包;

2:在web.xml中增加配置:

cxf与spring的整合

代码:

  <!--添加cxf配置文件-->

     <context-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>classpath:cxf.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>
</servlet>
<!-- 访问路由配置-->
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

3:cxf文件的配置:

注意:发布的接口和接口的具体实现都要贴上@WebService标签;

cxf与spring的整合

spring-cxf.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">
<!--引入源码中的配置文件--> <!--访问地址: http://localhost:8080/ws/product -->
<jaxws:server id="inlineImplementor" serviceClass="com.floor.shop.ws.IProductService"
address="/product">
<jaxws:serviceBean>
<bean class="com.floor.shop.ws.impl.ProductService"/>
</jaxws:serviceBean>
</jaxws:server> </beans>

二:客户端的相关配置:

0.拷贝jar包
1.在cmd中运行wsdl2java "http://localhost:8080/ws/product?wsdl 生成客户端代码
2.在spring配置文件中配置客户信息

在客户端项目的src文件夹下运行dos命令:

wsdl2java  +服务端提供的访问接口

cxf与spring的整合

2:客户端的   spring-cxf.xml    的配置:

cxf与spring的整合

代码:

 <?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"> <!-- 访问地址: http://localhost:8080/ws/product -->
<jaxws:client id="productService" serviceClass="com.floor.shop.ws.IProductService" address="http://localhost:8081/ws/product"/> </beans>

3:客户端中方法的调用:

cxf与spring的整合

代码:

  ClassPathXmlApplicationContext cxt =
new ClassPathXmlApplicationContext("spring/cxf_Client.xml"); IProductService productService = (IProductService)cxt.getBean("productService");
User user = new User();
user.setUserName("张无忌");
String s = productService.buyProduct(user);
System.out.println("s="+s);