且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

cxf和spring整合

更新时间:2022-08-21 18:27:43

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.