webservice与spring整合


转载地址:http://blog.csdn.net/mr_li13/article/details/51320738

前面的的提到了WebService整体开发过程,这里主要是说WebService与Spring的整合过程


一、提出问题

问题:在以往的项目中,我们都需要把项目发布到服务器上,而WebService也是一个服务,如果按照思想来说,我们需要发布两个服务才能使整个项目正常运行,而且端口号还不一样,这样服务端口,这不是显得特别的复杂。有没有一个办法使它发布服务的时候,webService服务也能开启,端口号用的是一个。显然我们是有办法的,我们将它和spring进行整合,统一交给Spring进行管理即可。


二、整合过程:

1)spring管理业务系统的bean,beans.xml(spring基础)
2) WebService CXF    cxf-servlet.xml(CXF服务文件)
3) 在web.xml中配置spring和cxf(这个肯定要在web.xml中加入撒)


1.考入jar包:这个jar包可以直接使用官网CXF下载的lib里面有spring的jar包,不用下载。点击下载链接

       新建一个WebProject工程,将jar包考入lib下

2.新建一个CXFService服务类,用以开启服务的。

  1. package com.itcast.ws;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /*import org.apache.cxf.jaxws.JaxWsServerFactoryBean;*/  
  6.   
  7. @WebService(serviceName="CXFServices")  
  8. public class CXFService {  
  9.       
  10.     public String hello(String name){  
  11.         System.out.println("已经进入服务器:"+name);  
  12.         return "从服务器返回值:"+name;  
  13.     }  
  14.       
  15. /*  public static void main(String[] args) { 
  16.         JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean(); 
  17.         bean.setAddress("http://localhost:9090/hello"); 
  18.         bean.setServiceClass(CXFServer.class); 
  19.         bean.create(); 
  20.         System.out.println("服务已经开启!"); 
  21.     }  */     
  22.     //传统的直接运行这个类就可以发布Service服务了。但是现在㤇直接和spring整个就不需要使用这个方式了  
  23. }  


3.1:我们先看看当cxf-servlet在WEB-INF下的时候配置:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.         xmlns:jaxws="http://cxf.apache.org/jaxws"   
  5.         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">  
  6.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  7.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  8.       
  9.     <!-- 有点儿类似JDK的形式  参数address是访问地址  implementor是实现者(最开始测试可以使用服务类com.itcast.ws.CXFService,但是后面和spring配置文件整合的时候就㤇使用bean的#ID了)-->  
  10.       
  11.     <jaxws:endpoint implementor="com.itcast.ws.CXFService" address="/hello"/>  
  12. </beans>  

3.2.再看看当把 cxf-servlet.xml和beans.xml在src下的时候配置:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.         xmlns:jaxws="http://cxf.apache.org/jaxws"   
  5.         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">  
  6.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  7.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  8.       
  9.     <!-- 有点儿类似JDK的形式  参数address是访问地址  implementor是实现者(最开始测试可以使用服务类com.itcast.ws.CXFService,但是后面和spring配置文件整合的时候就㤇使用bean的#ID了)-->  
  10.       
  11.     <jaxws:endpoint implementor="#cxfServices" address="/hello"/>  
  12.     <import resource="beans.xml"/>  
  13. </beans>  

4. beans.xml文件(Spring )

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.             http://www.springframework.org/schema/mvc   
  9.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
  10.             http://www.springframework.org/schema/context   
  11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  12.             http://www.springframework.org/schema/aop   
  13.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
  14.             http://www.springframework.org/schema/tx   
  15.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">  
  16.       
  17.     <!-- 这是Spring基础了,把服务类放进来,给spring管理 -->  
  18.     <bean id="cxfServices" class="com.itcast.ws.CXFService"></bean>   
  19.       
  20. </beans>            


5.web.xml文件配置信息:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.   <display-name></display-name>   
  9.   <!-- 整合spring servlet -->  
  10.     <context-param>  
  11.     <param-name>contextConfigLocation</param-name>  
  12.     <param-value>classpath:beans.xml</param-value>  
  13.   </context-param>  
  14.   <listener>  
  15.     <listener-class>org.springframework.web.context.ContextLoader</listener-class>  
  16.   </listener>  
  17.   
  18.     
  19.   <!-- CXF和servlet整合  只管理路径为cxf(/cxf/*)下的内容 走一下源码就知道它会找cxf-servlet.xml文件-->  
  20.   <servlet>  
  21.     <servlet-name>cxf</servlet-name>  
  22.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  23.     <!-- 如果cxf-servlet.xml在web-inf下的话,就不用配置,但是如果在src下需要配置一下  
  24.     名字为什么是config-location,看看CXFservlet源码就知道了。 -->  
  25.     <init-param>  
  26.         <param-name>config-location</param-name>  
  27.         <param-value>classpath:cxf-servlet.xml</param-value>  
  28.     </init-param>  
  29.   </servlet>  
  30.   <servlet-mapping>     
  31.   <!-- 访问WSDL文件的时候就需要访问这个名字,通过它可以找到web服务 -->  
  32.     <servlet-name>cxf</servlet-name>  
  33.     <url-pattern>/cxf/*</url-pattern>  
  34.   </servlet-mapping>  
  35.     
  36.   <welcome-file-list>  
  37.     <welcome-file>index.jsp</welcome-file>  
  38.   </welcome-file-list>  
  39. </web-app>  

这里就要提出一个问题了?它通过 cxf这个名字找到 org.apache.cxf.transport.servlet.CXFServlet后,怎么找到 cxf-servlet.xml文件的呢?我们追一下源码就知道了



tomcate运行下,看看结果:http://localhost:8080/CXFSpringProject/cxf

    


       


最后成功整合。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值