CXF3.1.6+Spring4.1.9整合

开发中经常碰到不同项目之间需要相互调用项目中已经成熟的方法来进行开发的情况。

这时候我选择的是WebService进行开发。由于之前调用WebService时使用过CXF这个框架,所以这次就继续选择了CXF来进行开发。

这里值得说明的是,我之前使用的是Spring3.1进行开发,所以想着直接整合进CXF3.1,在进行了两天的尝试无果后终于发现,原来需要高版本的Spring才能兼容CXF3.1.6,否则会报错,报的错误貌似是Spring3.1中的Core包中找不到ClassUtils.class中的isCglibProxyClass(Class<?>)方法,我在替换成Spring4.1后就解决了.


这里简单说一下Spring4.1与CXF3.1.6进行整合中简单的步骤及相关配置。

一、编写WebService接口类及其实现类

package WebService.IWebService;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import WebService.Polo.TelnetCommand;
import WebService.Polo.TelnetCommandResult;

@WebService
public interface ITestWebService {
	public List<TelnetCommandResult> sayHi(@WebParam(name="telnetCommandList") List<TelnetCommand> telnetCommandList);
}

这里需要说明@WebParam(name="telnetCommandList") 最好是编写上去,虽然不编写也不影响使用,但是在WSDL中将只能看到arg1、arg2等的参数名而不是具体参数名,影响接口的可读性。

@WebService也需要注明,否则调用时会报找不到相关接口的错误。


二、配置web.xml

  <!--spring监听  -->
  <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
  </listener>
  
  <!--CXF配置  -->
  <servlet>
  	<servlet-name>CXFService</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>CXFService</servlet-name>
  	<url-pattern>/SDDataWebService/*</url-pattern>
  </servlet-mapping>

需要在web.xml中加入上述配置,在tomcat启动时启用CXF服务,这里贴出的是最低配置,在这里还可以加入其他配置,暂不说明。


三、配置applicationContext.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-2.5.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
						
<!--配置CXF扫描,并制定包目录  -->
	<context:component-scan base-package="WebService"></context:component-scan>
	<import resource="applicationContext-cxf.xml"/>

需要在applicationContext.xml中加入上述配置,这里是指定了扫描的包路径以及将有关CXF的配置分离出来并制定配置文件的位置。


四、配置applicationContext-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-3.0.xsd
       	http://cxf.apache.org/jaxws  
       	http://cxf.apache.org/schemas/jaxws.xsd">
       	
    <bean id="testService" class="WebService.WebServiceImpl.TestServiceImpl"/>
    <!--測試Service  -->
	<jaxws:server serviceClass="WebService.IWebService.ITestWebService" address="/test">
		<jaxws:serviceBean>
			<ref bean="testService"/>
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

这里将有关CXF的配置从applicationContext.xml中分离出来,避免applicationContext.xml太过复杂,难以读懂。这里需要特别注意CXF所需生命的命名空间以及。


五、到这里就可以运行tomcat并访问http://localhost:8080/项目名称/SDDataWebService ,可以看到WebService有没有运行成功。

访问http://localhost:8080/项目名称/SDDataWebService/test?wsdl可以看到WebService的wsdl文件。





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值