在使用CXF写WebService的项目里 经常会和spring框架一起使用,下面将列出CXF如何在Spring框架中配置:
一、服务端配置:
1、首先项目导入CXF的jar包:org.apache.cxf:apache-cxf:3.1.3
2、在你spring配置文件的地方创建spring-cxf-servlet.xml文件 文件内容为:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <jaxws:client id="webServiceClient" serviceClass="com.ct10000.sc.interfaces.internal.cvp.service.webService.IWebService" address="http://localhost:8080/cvp/WsWebService" /> </beans>
3、在spring-core.xml文件里(spring核心配置文件)里 <import resource="spring-cxf-servlet.xml"/> 引入创建的xml文件
4、在WEB-INF目录下的web.xml配置文件里 写入
<!-- CXF servlet-->
<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>/*</url-pattern>
</servlet-mapping>
注意你的web.xml配置文件是否有上下文监听,如果没有写上以下代码:
<!-- 上下文监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-core.xml</param-value> </context-param>
二、客户端配置:
1、在客户端模块的spring配置的地方创建spring-cxf-client.xml文件,内容为:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <jaxws:client id="webServiceClient" serviceClass="com.ct10000.sc.interfaces.internal.cvp.service.webService.IWebService" address="http://localhost:8080/cvp/WsWebService" /> </beans>注意adress要和服务端的地址对应起来
2、客户端的代码:
package com.ct10000.sc.interfaces.internal.spp.webClient;
import com.ct10000.sc.entities.bean.commons.crm.qryProdInfo.ProdInfoBean;
import com.ct10000.sc.interfaces.internal.cvp.service.webService.IWebService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* webService 客户端
*
* Created by ryh on 2015/10/14.
*/
public class WebCilent {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf-client.xml");//指向客户端配置文件
IWebService service = context .getBean("webServiceClient", IWebService.class);
String number = "";
String numberKey = "phoneNumber";
List<ProdInfoBean> List = service.findProdInfoByNumber(number,numberKey);
}
}
这样客户端的配置也就完成了 ,希望对大家有所帮助。