• cxf整合到spring中


          项目中一般都是通过spring来进行管理,因此,绝大多数框架最终都将整合的spring中,cxf也不例外。

         下面就讲解如何配置cxf,将其整合到spring中。(例子中web项目通过maven来进行管理,建立的mav项目为WebService_CXF)

    一、pom.xml文件添加依赖jar包  

         1)添加spring支持
         2)添加cxf支持

        配置如下:     

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3   <modelVersion>4.0.0</modelVersion>
      4   <groupId>com.hik.CXF</groupId>
      5   <artifactId>WebService_CXF</artifactId>
      6   <packaging>war</packaging>
      7   <version>0.0.1-SNAPSHOT</version>
      8   <name>WebService_CXF Maven Webapp</name>
      9   <url>http://maven.apache.org</url>
     10   <dependencies>
     11     <dependency>
     12       <groupId>junit</groupId>
     13       <artifactId>junit</artifactId>
     14       <version>4.8.1</version>
     15       <scope>test</scope>
     16     </dependency>
     17     
     18     <!--添加servlet支持  -->
     19     <dependency>
     20         <groupId>javax.servlet</groupId>
     21         <artifactId>javax.servlet-api</artifactId>
     22         <version>3.1.0</version>
     23         <scope>provided</scope>
     24     </dependency>
     25     <dependency>
     26         <groupId>javax.servlet.jsp</groupId>
     27         <artifactId>javax.servlet.jsp-api</artifactId>
     28         <version>2.3.1</version>
     29         <scope>provided</scope>
     30     </dependency>
     31     
     32     <!-- 添加Spring支持 -->
     33     <dependency>
     34           <groupId>org.springframework</groupId>
     35           <artifactId>spring-core</artifactId>
     36           <version>4.1.7.RELEASE</version>
     37       </dependency>
     38       <dependency>
     39           <groupId>org.springframework</groupId>
     40           <artifactId>spring-beans</artifactId>
     41           <version>4.1.7.RELEASE</version>
     42       </dependency>
     43       <dependency>
     44          <groupId>org.springframework</groupId>
     45          <artifactId>spring-tx</artifactId>
     46          <version>4.1.7.RELEASE</version>
     47         </dependency>
     48       <dependency>
     49           <groupId>org.springframework</groupId>
     50           <artifactId>spring-context</artifactId>
     51           <version>4.1.7.RELEASE</version>
     52       </dependency>
     53       <dependency>
     54           <groupId>org.springframework</groupId>
     55           <artifactId>spring-context-support</artifactId>
     56           <version>4.1.7.RELEASE</version>
     57       </dependency>
     58       
     59       <dependency>
     60         <groupId>org.springframework</groupId>
     61         <artifactId>spring-web</artifactId>
     62         <version>4.1.7.RELEASE</version>
     63     </dependency>
     64     
     65     <dependency>
     66         <groupId>org.springframework</groupId>
     67         <artifactId>spring-webmvc</artifactId>
     68         <version>4.1.7.RELEASE</version>
     69     </dependency>
     70     
     71     <dependency>
     72         <groupId>org.springframework</groupId>
     73         <artifactId>spring-aop</artifactId>
     74         <version>4.1.7.RELEASE</version>
     75     </dependency>
     76     
     77     <dependency>
     78         <groupId>org.springframework</groupId>
     79         <artifactId>spring-aspects</artifactId>
     80         <version>4.1.7.RELEASE</version>
     81     </dependency>
     82     
     83     <dependency>
     84         <groupId>org.springframework</groupId>
     85         <artifactId>spring-jdbc</artifactId>
     86         <version>4.1.7.RELEASE</version>
     87     </dependency>
     88     
     89     <!--添加cxf支持  -->
     90     <dependency>
     91         <groupId>org.apache.cxf</groupId>
     92         <artifactId>cxf-rt-frontend-jaxws</artifactId>
     93         <version>3.1.9</version>
     94     </dependency>
     95     <!--在tomcat服务器上执行  -->
     96     <dependency>
     97         <groupId>org.apache.cxf</groupId>
     98         <artifactId>cxf-rt-transports-http</artifactId>
     99         <version>3.1.9</version>
    100     </dependency>
    101     <dependency>
    102         <groupId>org.apache.cxf</groupId>
    103         <artifactId>cxf-core</artifactId>
    104         <version>3.1.9</version>
    105     </dependency>
    106     
    107   </dependencies>
    108   <build>
    109     <finalName>WebService_CXF</finalName>
    110   </build>
    111 </project>
    View Code

    二、spring加载配置文件 applicationContext.xml 配置

           1)配置文件添加jaxws支持(cxf)标签

           2)添加cxf的一些bean配置,主要是cxf的servlet处理请求加载用到 CXFServlet

           3)Spring配置文件里,定义下webservice接口发布

       配置如下:

      

     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:p="http://www.springframework.org/schema/p"  
     5     xmlns:aop="http://www.springframework.org/schema/aop"   
     6     xmlns:context="http://www.springframework.org/schema/context"  
     7     xmlns:jee="http://www.springframework.org/schema/jee"  
     8     xmlns:tx="http://www.springframework.org/schema/tx"  
     9     xmlns:jaxws="http://cxf.apache.org/jaxws"
    10     xsi:schemaLocation="    
    11         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    12         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    13         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
    14         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
    15         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    16         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">    
    17         
    18      <!-- 自动扫描 -->
    19     <context:component-scan base-package="com.hik.webservice" />
    20     
    21     <import resource="classpath:META-INF/cxf/cxf.xml"/>
    22     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    23     <!-- 定义服务提供者  -->
    24     <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld"></jaxws:endpoint>
    25     
    26 </beans>
    View Code

     详细说明图:

    三、web.xml配置

          1)添加spring加载,spring监听支持,web启动时加载spring

          2)定义一个Servlet,主要是处理WebService请求。所有的 /webservice请求,都交给CXFServlet类处理;

         配置如下:

        

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>TestWeb</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   
    13   <!-- 添加对spring的支持 ,spring配置文件-->
    14   <context-param>
    15         <param-name>contextConfigLocation</param-name>
    16         <param-value>classpath:applicationContext.xml</param-value>
    17   </context-param>
    18   <!-- 定义Spring监听器,加载Spring  -->
    19   <listener>
    20         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    21   </listener>
    22   
    23     <!-- 定义一个Servlet,主要是处理WebService请求 -->
    24     <servlet>  
    25          <servlet-name>CXFServlet</servlet-name>  
    26          <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    27     </servlet>  
    28     <servlet-mapping>  
    29         <servlet-name>CXFServlet</servlet-name>  
    30         <url-pattern>/webservice/*</url-pattern>  
    31     </servlet-mapping>
    32   
    33   
    34 </web-app>
    View Code

    四、启动项目访问测试

         启动下项目,然后访问 http://localhost:8080/WebService_CXF/webservice/ 

    说明已经webservice接口已经发布成功了;

    我们可以访问具体的接口: http://localhost:8080/WebService_CXF/webservice/HelloWorld?wsdl

    效果如下:

    附加拦截器配置(applicationContext.xml)说明截图:

       

  • 相关阅读:
    Id_Name
    注入类型(Injection Type)
    搭建 Spring 2.5.6 开发环境
    模拟Spring框架
    AJAX 学习笔记 2017_05_04
    Gson 转换hibernate级联对象出现StackOverFlow(堆栈溢出)问题
    页面左侧可收缩菜单案例
    struts1.3.10 和 hibernate 3.3.2 一起加载时 出现javax.servlet.ServletException: java.lang.NoSuchMethodError: antlr.collections.A 错误
    1.21 Java基础总结 数组知识
    1.20 Java基础总结 输入数据类型判断 Java低耦合原则 for嵌套思路
  • 原文地址:https://www.cnblogs.com/jedjia/p/cxf_spring.html
Copyright © 2020-2023  润新知