`

cxf和spring整合

 
阅读更多

spring 3.0 cxf2.7.2整合

1,拷贝spring的 jar
2,拷贝cxf的jar包
    jetty不需要了
    asm
    common-logging
    neethi
    wsdl4j
    xmlschema
    cxf
    http-*
3,修改web.xml 添加对spring的支持
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INFO/applicationContext.xml</param-value>
    </context-param>
    <listener-class>
        ContentLoadListener
    </listener-class>
4,在web.xml添加对cxf的支持
    //配置cxf的核心控制器
    <servlet>
        <servlet-name>cxf
        <servlet-class>CXFServlet
    </servlet>
    //所有来自/ws/*的请求交给cxf处理
    <servlet-mapping>
        <servlet-name>cxf
        <url-pattern>/ws/*
    </servlet-mapping>   
5,在spring中倒入schema和xml配置
    在cxf.jar/schema中可以看到 jaxws.xsd 找到命名空间
    在cxf.jar/META-INF/spring.schemas  找到schema-location
        http\://cxf.apache.org/schemas/jaxws.xsd=schemas/jaxws.xsd
    在cxf.jar/META-INF/这里存在一些xml配置文件
        在spring需要导入一些配置文件
        <import resourse="classpath:/META-INF/xxx.xml"/>
            cxf.xml
        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


        web应用的类加载路径有两类:
            1,WEB-INF/classes目录
            2,WEB-INF/lib目录
6,在spring中使用jaxws:endpoint元素来暴露Web Service
    implementor指定web service的服务提供者,支持两种形式:
    1,直接给服务器提供类名
    2,设置为spring容器中的一个bean
      <jaxws:endpoint
        implementor="xxx.xx.xx.Xxx" 
            address="/ws" >
    </jaxws:endpoint>
    第二种方式
    <bean  id="webServiceName" class="xxx.xx.xx.Xxx" >
         <ref bean="customerService" />
    </bean>
      <jaxws:endpoint
        implementor="#webServiceName"  #代表使用spring容器中的类
            address="/ws" >
        <jaxws:inInterceptors>
            <bean class="xx.xx.xx.Xxx"/>
            <bean class="xx.xx.xx.Xxx"/>
        </jaxws:inInterceptors>
    </jaxws:endpoint>

cxf和Spring的另外一种整合:在action调用web service
1,复制cxf jar包
2,在spring中配置导入cxf提供的schema,xml配置文件(jaxws)
3,在spring中使用jaxws:client来配置webservice
    <jaxws:client id="hw"
        serviceClass=""
        address="http://localhost:8080/ws_03_server_cxf_spring3.1/ws?wsdl">
        <jaxws:outInterceptors>
            <bean class="">
                <contractor-arg value="admin" />
                <contractor-arg value="admin" />
            </bean>
        </jaxws:outInterceptors>
    </jaxws:client>
4,添加拦截器方法一样


服务器端

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0"  
  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_3_0.xsd"> 
  7.   <display-name></display-name>  
  8.   <welcome-file-list> 
  9.     <welcome-file>index.jsp</welcome-file> 
  10.   </welcome-file-list> 
  11.     <context-param> 
  12.         <param-name>contextConfigLocation</param-name> 
  13.         <param-value>/WEB-INF/applicationContext.xml</param-value> 
  14.     </context-param> 
  15.     <listener> 
  16.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  17.     </listener> 
  18.      
  19.     <servlet> 
  20.         <servlet-name>CXF</servlet-name> 
  21.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
  22.     </servlet> 
  23.     <!-- 所有来自/ws/*的请求交给cxf处理 --> 
  24.     <servlet-mapping> 
  25.         <servlet-name>CXF</servlet-name> 
  26.         <url-pattern>/ws/*</url-pattern> 
  27.     </servlet-mapping>   
  28.    
  29. </web-app> 

spring配置applicationContext.xml

 

  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:oxm="http://www.springframework.org/schema/oxm" 
  5.     xmlns:jaxws="http://cxf.apache.org/jaxws" 
  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/oxm 
  9.     http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd 
  10.     http://cxf.apache.org/jaxws 
  11.     http://cxf.apache.org/schemas/jaxws.xsd"> 
  12.              
  13.             <import resource="classpath:META-INF/cxf/cxf.xml" /> 
  14.             <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
  15.             <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
  16.              
  17.              <!-- 
  18.              第一种方式   
  19.              <jaxws:endpoint  
  20.                 implementor="com.kenan.cxf.ws.impl.HelloWorldImpl"   
  21.                 address="/ws" > 
  22.             </jaxws:endpoint> --> 
  23.             <!-- 第二种方式 --> 
  24.             <bean  id="webServiceName" class="com.kenan.cxf.ws.impl.HelloWorldImpl" > 
  25.             </bean> 
  26.             <!-- #代表使用spring容器中的类 --> 
  27.       <jaxws:endpoint  
  28.             implementor="#webServiceName"   
  29.             address="/ws" > 
  30.         <jaxws:inInterceptors> 
  31.             <bean class="com.kenan.cxf.auth.AuthInterceptor"/> 
  32.         </jaxws:inInterceptors> 
  33.     </jaxws:endpoint> 
  34.      
  35. </beans> 

spring客户端

applicationContext.xml
 

  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:oxm="http://www.springframework.org/schema/oxm" 
  5.     xmlns:jaxws="http://cxf.apache.org/jaxws" 
  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/oxm 
  9.     http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd 
  10.     http://cxf.apache.org/jaxws 
  11.     http://cxf.apache.org/schemas/jaxws.xsd"> 
  12.                          
  13.      
  14.     <jaxws:client id="hw" 
  15.             serviceClass="com.kenan.cxf.ws.HelloWorld" 
  16.         address="http://localhost:8080/ws_03_server_cxf_spring3.1/ws/ws?wsdl"> 
  17.             <jaxws:outInterceptors> 
  18.                 <bean class="com.kenan.cxf.auth.AuthOutInterceptor"> 
  19.                     <constructor-arg value="admin" /> 
  20.                     <constructor-arg value="admin" /> 
  21.                 </bean> 
  22.             </jaxws:outInterceptors> 
  23.     </jaxws:client> 
  24. </beans> 

测试
   

  1. package test; 
  2.  
  3. import org.springframework.context.ApplicationContext; 
  4. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5.  
  6. import com.kenan.cxf.ws.HelloWorld; 
  7. import com.kenan.cxf.ws.impl.HelloWorldImpl; 
  8. import com.sun.security.ntlm.Client; 
  9.  
  10. public class Test { 
  11.  
  12.     /** 
  13.      * @param args 
  14.      */ 
  15.     public static void main(String[] args) { 
  16.          
  17.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
  18.         HelloWorld hello = context.getBean("hw", HelloWorld.class); 
  19.          
  20.          
  21. //      HelloWorldImpl helloWorld = new HelloWorldImpl(); 
  22. //      com.kenan.cxf.ws.HelloWorld hello = helloWorld.getHelloWorldImplPort(); 
  23.          
  24.         //拦截器 
  25. //      Client client = ClientProxy.getClient(hello); 
  26. //      client.getOutInterceptors().add(new AuthOutInterceptor("admin", "admin")); 
  27. //      client.getOutInterceptors().add(new LoggingOutInterceptor()); 
  28.          
  29.         hello.hello("你好"); 
  30.          
  31.          
  32.          
  33. //      User user = new User(); 
  34. //      user.setName("柯南"); 
  35. //      List<Cat> l = hello.getCatsByUser(user); 
  36. //      for(Cat cat:l){ 
  37. //          System.out.println(cat.getName()); 
  38. //      } 
  39. //      StringCat s = hello.getAllCats(); 
  40. //      for(Entry entry:s.getEntries()){ 
  41. //          System.out.println(entry.getKey()+":"+entry.getValue()); 
  42. //      } 
  43.     } 
  44.  
分享到:
评论

相关推荐

    CXF和Spring整合,并且添加拦截器

    **标题:“CXF和Spring整合,并且添加拦截器”** 在Java世界中,Apache CXF是一个流行的开源服务框架,用于创建和消费Web服务。它支持多种Web服务规范,包括SOAP、RESTful API以及WS-*标准。另一方面,Spring框架是...

    CXF和spring整合所需jar包

    这里我们将深入探讨CXF与Spring整合时所需的jar包及其作用。 首先,我们需要理解整合的目的:通过Spring管理CXF的服务生命周期,使得服务的创建、配置和管理更加便捷。为了实现这一目标,我们需要以下关键的jar包:...

    cxf整合spring

    将CXF与Spring整合的主要好处在于,可以利用Spring的强大管理和配置能力来管理CXF服务。Spring的DI可以帮助我们将CXF的服务bean与其他业务逻辑bean解耦,从而提高代码的可测试性和可维护性。此外,Spring还提供了对...

    CXF和Spring整合开发的服务端及客户端

    **标题解析:** "CXF和Spring整合开发的服务端及客户端" 指的是使用Apache CXF框架与Spring框架相结合,构建服务端(服务提供者)和服务端客户端(服务消费者)。CXF是一个开源的Web服务框架,它允许开发者创建和...

    cxf+spring整合

    "CXF+Spring整合"是企业级Java应用程序中常用的一种技术组合,它结合了Apache CXF(一个用于构建和服务导向架构的开源框架)和Spring框架的优势,以实现高效、灵活的服务开发和管理。在Java世界里,CXF常用于创建Web...

    WebService的CXF整合Spring

    将CXF与Spring整合的主要目的是利用Spring的管理能力来配置和控制CXF组件,例如服务端点、客户端代理等。这样可以实现更细粒度的控制,提高代码的可测试性和可维护性。 整合步骤如下: 1. **引入依赖**:首先,在...

    CXF整合spring的客户端和服务端

    将CXF与Spring整合,可以充分利用Spring的管理能力,简化服务的创建和维护。 1. **整合步骤** - **配置Spring**:首先,我们需要在Spring配置文件中定义CXF的相关bean,如服务接口的实现类、服务端点、服务发布器...

    CXF整合Spring步骤

    在企业级应用开发中,Apache CXF 和 Spring 框架的整合是非常常见的,因为它们分别提供了强大的服务端和客户端的 Web 服务支持以及灵活的依赖注入和配置管理。本教程将详细介绍如何将 CXF 与 Spring 整合,帮助...

    cxf_spring 相关 jar

    解压这个"spring-cxf"压缩包后,你会找到一系列的JAR文件,这些文件包含了CXF和Spring整合所需的类和库。例如,可能包含的有: 1. `cxf-api.jar`:CXF的核心API,包含了服务接口和数据绑定相关的类。 2. `cxf-rt-...

    13.为CXF与Spring整合发布WebService添加拦截器进行权限控制

    当我们需要在CXF和Spring整合的基础上发布Web服务,并对权限进行控制时,可以利用拦截器来实现这一目标。本文将详细介绍如何为CXF与Spring整合发布WebService添加拦截器进行权限控制。 首先,我们需要理解CXF拦截器...

    cxf与Spring的整合实例(适合初学者)

    **标题解析:** "cxf与Spring的整合实例(适合初学者)" 指的是一个教程或示例项目,旨在帮助初次接触CXF和Spring框架的开发者理解如何将这两个强大的开源工具集成在一起。CXF是一个流行的开源服务框架,常用于构建...

    webservice(cxf)与spring整合源码+文档

    标题"webservice(cxf)与spring整合源码+文档"表明这是一个关于如何将CXF Web服务与Spring框架集成的学习资源。文档和源代码一起提供,使得学习者能够通过实践来理解这一过程。 描述中提到"webservice与spring整合...

    CXF-Spring相关jar包

    在【压缩包子文件的文件名称列表】中,"cxf-spring"可能包含的是用于集成CXF与Spring的必要jar包,如cxf-spring-integration.jar,这个文件通常包含了CXF与Spring集成所需的类和资源,使得开发者能够在Spring环境下...

    cxf spring maven 实例

    5. **Spring整合CXF**:Spring可以通过`cxf-spring`模块来集成CXF,通过`&lt;jaxws:endpoint&gt;`或`&lt;jaxrs:server&gt;`标签在Spring配置文件中声明Web服务。这样,Spring容器会管理Web服务的生命周期,服务实例可以在需要时...

    CXF与Spring 2.5整合

    2. **CXF的Spring支持**:CXF提供了对Spring的深度集成,可以通过Spring的XML配置或注解来定义和管理Web服务。例如,使用`&lt;jaxws:endpoint&gt;`标签创建一个基于JAX-WS的服务端点,或者使用`&lt;jaxrs:server&gt;`创建基于JAX...

    Apache CXF 与 Spring 整合简单例子

    这篇博客文章可能是一个简单的教程,教你如何将Apache CXF和Spring整合在一起,创建一个基本的服务端点。 在整合Apache CXF和Spring时,首先需要在Spring配置文件中声明CXF的相关bean。这通常包括服务接口、服务...

    CXF 和 Spring集成所需jar包

    cxf-2.2.9.jar geronimo-javamail_1.4_spec-1.7.1.jar geronimo-jaxws_2.2_spec-1.1.jar geronimo-jms_1.1_spec-1.1.1.jar geronimo-servlet_3.0_spec-1.0.jar jaxb-api-2.2.6.jar jaxb-impl-2.2.6.jar neethi-3.0.1...

Global site tag (gtag.js) - Google Analytics