`

webservice学习 5.spring与CXF集成

阅读更多

环境:CXF 2.6.3

IDE: eclipse for javaEE JUNO

 

这系列文章都是看书看视频的笔记 如果有错误欢迎指正^_^

 

过程是比较简单的。CXF的程序用现成的 不讲如何开发JAX-WS或者JAX-RS

 

 

 

1.创建项目(动态web项目)在 web-inf/下导入CXF和其他第三方包


 

 

2.修改web.xml 增加spring配置:

在web.xml中增加启动spring的配置和CXF的有关配置

配置内容如下:

 

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
  <display-name>spring_cxf</display-name>
  
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/beans-server.xml</param-value>
  </context-param>
  <listener>
    <listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
  </listener>
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>

</web-app>

 beans-server.xml是指定的spring配置文件(具体解释可以看:http://blog.csdn.net/sapphire_aling/article/details/6069764)

 

 

beans-server.xml的内容如下,在spring的配置中增加CXF的内容:

 

<?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"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	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://cxf.apache.org/jaxrs
	http://cxf.apache.org/schemas/jaxrs.xsd">
	
	<!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->
	<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" />
  
  	<!-- CXF 提供的内置拦截器 -->
  	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
  	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
  	
  
  	<!-- 配置方案一  使用JAX-WS标准配置jaxws:endpoint,发布服务 -->
  	<!-- id:spring bean标识,implementor:服务实现类,address:服务发布路径 -->
	<jaxws:endpoint id="merchant_1" 
			implementor="com.cxfdemo.server.service.impl.IMerchantServiceImpl" address="/m_1">
		<!-- 可选配置入口拦截器 -->
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<!-- 可选配置出口拦截器 -->
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
	</jaxws:endpoint>
	
	<!-- 配置方案二  使用JAX-WS标准配置jaxws:endpoint,发布服务 -->
	<!-- id:spring bean标识,serviceClass:服务实现接口,address:服务发布路径 -->
	<jaxws:server id="merchant_2" serviceClass="com.cxfdemo.server.service.IMerchantService" address="/m_2">
		<!-- 注入:服务实现类 -->
		<jaxws:serviceBean>
			<ref bean="merchantService"/>
		</jaxws:serviceBean>	
		
		<!-- 可选配置入口拦截器 -->
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<!-- 可选配置出口拦截器 -->
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
		
	</jaxws:server>
	<!-- 服务实现类 -->
	<bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" />

</beans>

 以上有两种方式

 

  • 使用jaxws:endpoint 和代码里使用EndPoint是差不多的 配置服务实现类和地址即可(也可以增加拦截器等;
  • jaxws:server 方式 这和JaxWsServerFactoryBean的使用差不多 配置服务类配置地址 还有服务的实现类

以上完成就可以运行了:



 可见运行正常

 

 

JAX-RS同理 只需要修改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"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	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://cxf.apache.org/jaxrs
	http://cxf.apache.org/schemas/jaxrs.xsd">
	
	<!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->
	<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" />
  
  	<!-- CXF 提供的内置拦截器 -->
  	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
  	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
  	

  	<jaxrs:server id="merchantrs" address="/">
  		<jaxrs:serviceBeans>
  			<ref bean="merchantService"/>
  		</jaxrs:serviceBeans>
 		
 		<jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
        
		<!-- 可选配置入口拦截器 -->
		<jaxrs:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxrs:inInterceptors>
		<!-- 可选配置出口拦截器 -->
		<jaxrs:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxrs:outInterceptors>		
  	</jaxrs:server>
  
	<!-- 服务实现类 -->
	<bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" />

</beans>

 

以上的代码来自北风网的视频课程的源代码(太懒了就没自己重写个webservice) 这里以它的代码来做笔记用(不是广告的说..哎~)

 

  • 大小: 22.1 KB
  • 大小: 62.7 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics