<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

    五、CXF WebService整合Spring

    首先,CXF和spring整合需要準備如下jar包文件:

    image

    這邊我是用Spring的jar包是Spring官方提供的,并沒有使用CXF中的Spring的jar文件。

    添加這么多文件后,首先在web.xml中添加如下配置:

    <!-- 加載Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 設置Spring容器加載配置文件路徑 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-server.xml</param-value>
    </context-param>
     
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
     
    <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
     
    <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    然后在src目錄中,新建一個applicationContext-server.xml文件,文件內容如下:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans >
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd"

    注意上面的帶下劃線加粗部分,這個很重要的哦!不能寫錯或是遺漏了。

    添加完這個文件后,還需要在這個文件中導入這么幾個文件。文件內容如下:

    <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"/>

    下面開始寫服務器端代碼,首先定制服務器端的接口,代碼如下:

    package com.hoo.service;
     
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    import com.hoo.entity.User;
    import com.hoo.entity.Users;
     
    /**
     * <b>function:</b>定制客戶端請求WebService所需要的接口
     * @author hoojo
     * @createDate 2011-3-18 上午08:22:55
     * @file ComplexUserService.java
     * @package com.hoo.service
     * @project CXFWebService
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    @WebService
    @SOAPBinding(style = Style.RPC)
    public interface IComplexUserService {
        
        public User getUserByName(@WebParam(name = "name") String name);
        
        public void setUser(User user);
    }

    下面編寫WebService的實現類,服務器端實現代碼如下:

    package com.hoo.service;
     
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    import com.hoo.entity.User;
    import com.hoo.entity.Users;
     
    /**
     * <b>function:</b> WebService傳遞復雜對象,如JavaBean、Array、List、Map等
     * @author hoojo
     * @createDate 2011-3-18 上午08:22:55
     * @file ComplexUserService.java
     * @package com.hoo.service
     * @project CXFWebService
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    @WebService
    @SOAPBinding(style = Style.RPC)
    @SuppressWarnings("deprecation")
    public class ComplexUserService implements IComplexUserService {
        
        public User getUserByName(@WebParam(name = "name") String name) {
            User user = new User();
            user.setId(new Date().getSeconds());
            user.setName(name);
            user.setAddress("china");
            user.setEmail(name + "@hoo.com");
            return user;
        }
        
        public void setUser(User user) {
            System.out.println("############Server setUser###########");
            System.out.println("setUser:" + user);
        }
    }

    注意的是和Spring集成,這里一定要完成接口實現,如果沒有接口的話會有錯誤的。

    下面要在applicationContext-server.xml文件中添加如下配置:

    <bean id="userServiceBean" class="com.hoo.service.ComplexUserService"/>
     
    <bean id="inMessageInterceptor" class="com.hoo.interceptor.MessageInterceptor">
        <constructor-arg  value="receive"/>
    </bean>
     
    <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
    <!-- 注意下面的address,這里的address的名稱就是訪問的WebService的name -->
    <jaxws:server id="userService" serviceClass="com.hoo.service.IComplexUserService" address="/Users">
        <jaxws:serviceBean>
            <!-- 要暴露的 bean 的引用 -->
            <ref bean="userServiceBean"/>
        </jaxws:serviceBean>
        <jaxws:inInterceptors>
            <ref bean="inMessageInterceptor"/>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <ref bean="outLoggingInterceptor"/>
        </jaxws:outInterceptors>
    </jaxws:server>

    下面啟動tomcat服務器后,在WebBrowser中請求:

    http://localhost:8080/CXFWebService/Users?wsdl

    如果你能看到wsdl的xml文件的內容,就說明你成功了,注意的是上面地址的Users就是上面xml配置中的address的名稱,是一一對應的。

    下面編寫客戶端請求的代碼,代碼如下:

    package com.hoo.client;
     
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import com.hoo.entity.User;
    import com.hoo.service.IComplexUserService;
     
    /**
     * <b>function:</b>請求Spring整合CXF的WebService客戶端
     * @author hoojo
     * @createDate 2011-3-28 下午03:20:35
     * @file SpringUsersWsClient.java
     * @package com.hoo.client
     * @project CXFWebService
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    public class SpringUsersWsClient {
     
        public static void main(String[] args) {
            //調用WebService
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.setServiceClass(IComplexUserService.class);
            factory.setAddress("http://localhost:8080/CXFWebService/Users");
            
            IComplexUserService service = (IComplexUserService) factory.create();
            
            System.out.println("#############Client getUserByName##############");
            User user = service.getUserByName("hoojo");
            System.out.println(user);
            
            user.setAddress("China-Guangzhou");
            service.setUser(user);
        }
    }

    運行后,可以在控制臺中看到

    log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    2011-3-28 18:12:26 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
    信息: Creating Service {http://service.hoo.com/}IComplexUserServiceService from class com.hoo.service.IComplexUserService
    #############Client getUserByName##############
    27#hoojo#hoojo@hoo.com#china
    
    Tomcat控制臺
     image 

    這個server端是通過Spring整合配置的,下面我們將Client端也通過Spring配置完成整合。

    首先增加applicationContext-client.xml配置文件,文件內容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans >
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd"
        
        <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"/>
        
        <jaxws:client id="userWsClient" serviceClass="com.hoo.service.IComplexUserService" 
            address="http://localhost:8080/CXFWebService/Users"/>
    </beans>

    客戶端請求代碼如下:

    package com.hoo.client;
     
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.hoo.entity.User;
    import com.hoo.service.IComplexUserService;
     
    /**
     * <b>function:</b>請求Spring整合CXF的WebService客戶端
     * @author hoojo
     * @createDate 2011-3-28 下午03:20:35
     * @file SpringUsersWsClient.java
     * @package com.hoo.client
     * @project CXFWebService
     * @blog http://blog.csdn.net/IBM_hoojo
     * @email hoojo_@126.com
     * @version 1.0
     */
    public class SpringUsersWsClient {
     
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
            
            IComplexUserService service = ctx.getBean("userWsClient", IComplexUserService.class);
            
            System.out.println("#############Client getUserByName##############");
            User user = service.getUserByName("hoojo");
            System.out.println(user);
            
            user.setAddress("China-Guangzhou");
            service.setUser(user);
        }
    }

    運行后結果如下:

    #############Client getUserByName##############
    45#hoojo#hoojo@hoo.com#china
    ############Server setUser###########
    setUser:45#hoojo#hoojo@hoo.com#China-Guangzhou

    作者:hoojo
    出處:

    http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html
    blog:http://blog.csdn.net/IBM_hoojo
    本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

     


    版權所有,轉載請注明出處 本文出自: http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html
    posted on 2012-08-21 11:17 abin 閱讀(1600) 評論(0)  編輯  收藏 所屬分類: springcxf
    主站蜘蛛池模板: 亚洲久本草在线中文字幕| 久久久www成人免费毛片| 亚洲卡一卡2卡三卡4卡无卡三| 中文字幕乱码免费视频| 亚洲αv在线精品糸列| 精品一区二区三区免费观看| 久久精品亚洲中文字幕无码麻豆 | a一级毛片免费高清在线| 亚洲久本草在线中文字幕| 日本免费电影一区| 亚洲AV成人无码网天堂| 亚洲精品高清无码视频| 精品国产一区二区三区免费看| 免费无码又爽又刺激网站直播 | 国产亚洲漂亮白嫩美女在线| 亚洲AV无码成人专区片在线观看| 嫩草影院在线免费观看| 亚洲午夜免费视频| 人体大胆做受免费视频| 亚洲一区二区三区写真 | 亚洲成a人无码亚洲成av无码| 亚洲色无码一区二区三区| 成人性生交大片免费看好| 亚洲欧美自偷自拍另类视| 91亚洲国产成人精品下载| 亚洲男人第一无码aⅴ网站| 18禁无遮挡无码网站免费| 一级毛片免费观看不卡的| 亚欧国产一级在线免费| 亚洲欧美日韩中文高清www777| 综合自拍亚洲综合图不卡区| 中文字幕亚洲乱码熟女一区二区 | 亚洲最大的成人网站| 亚洲资源在线观看| a级亚洲片精品久久久久久久| 国产片免费在线观看| 91av免费在线视频| 色偷偷亚洲第一综合| 国产成人精品日本亚洲专一区| 四虎永久成人免费| 99视频在线精品免费观看6|