webservice概述及cxf在Java开发中应用(六) cxf与spring集成

    首先我们建一个web项目,导入cxf包,spring的包。注意这里不用加入jetty的包,因为我们用了tomcat

    修改web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" 
	 	version="2.5">
	 	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<!-- 启动加载spring容器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置字符集过滤 -->
  <filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
  <!-- 以下配置表名所有来自 /services/*的请求都交给CXFServlet处理-->
  <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>/services/*</url-pattern>
  </servlet-mapping>
</web-app>

    servlet的意义在于对于services路径下的请求,我们交给cxf控制器处理。

    然后我们在建一个applicationContext.xml配置文件,在配置文件中导入CXF提供的Schema,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:context="http://www.springframework.org/schema/context"
	   xmlns:aop="http://www.springframework.org/schema/aop" 
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.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
	http://cxf.apache.org/jaxws
	http://cxf.apache.org/schemas/jaxws.xsd"
	>
	
	<import resource="classpath:META-INF/cxf/cxf.xml" />  

    <bean id="helloService" class="com.dqjb.ws.imlp.HelloWorldImpl"> 
    </bean>
    
    <!-- implementor指定webservice服务提供者。支持两种形式:
    	A.直接给定服务提供者类名	com.dqjb.ws.imlp.HelloWorldImpl
    	B.设定容器中一个Bean     #helloService-->
    <jaxws:endpoint
    	implementor="#helloService"
    	address="/HelloWorld"
    >
    	<!-- 添加两个In拦截器 -->
    	<jaxws:inInterceptors>
    		<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
    		<bean class="com.dqjb.util.AuthIntercepter" />
    	</jaxws:inInterceptors>
    	
    	<jaxws:outInterceptors>
    		<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
    	</jaxws:outInterceptors>
    </jaxws:endpoint>
    <!-- 客户端 -->
<!--     <jaxws:client id="abc" serviceClass="com.cc.ABC" address="http://127.0.0.0:8080/webservice/abc">
    	<jaxws:outInterceptors>
    	</jaxws:outInterceptors>
    </jaxws:client> -->
</beans>

    其中import我们只引入了cxf.xml这一个XML文件,而在老版本中需要引入三个(cxf3.0.4中没有另外两个文件,引入会报错):

    <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" />

    然后启动tomcat,在浏览器输入地址********/services,就可以看到一个接口列表,点开相应接口,即可看到wsdl文档,表明与spring集成发布成功。

161522_qHRm_252483.png

转载于:https://my.oschina.net/wujialing/blog/411862

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值