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

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

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

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

    五、CXF WebService整合Spring

    首先,CXF和spring整合需要準(zhǔn)備如下jar包文件:

    image

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

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

    <!-- 加載Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 設(shè)置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目錄中,新建一個(gè)applicationContext-server.xml文件,文件內(nèi)容如下:

     

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

    注意上面的帶下劃線加粗部分,這個(gè)很重要的哦!不能寫(xiě)錯(cuò)或是遺漏了。

    添加完這個(gè)文件后,還需要在這個(gè)文件中導(dǎo)入這么幾個(gè)文件。文件內(nèi)容如下:

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

    下面開(kāi)始寫(xiě)服務(wù)器端代碼,首先定制服務(wù)器端的接口,代碼如下:

    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>定制客戶(hù)端請(qǐng)求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);
    }

    下面編寫(xiě)WebService的實(shí)現(xiàn)類(lèi),服務(wù)器端實(shí)現(xiàn)代碼如下:

    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傳遞復(fù)雜對(duì)象,如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集成,這里一定要完成接口實(shí)現(xiàn),如果沒(méi)有接口的話會(huì)有錯(cuò)誤的。

    下面要在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的名稱(chēng)就是訪問(wèn)的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>

    下面啟動(dòng)tomcat服務(wù)器后,在WebBrowser中請(qǐng)求:

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

    如果你能看到wsdl的xml文件的內(nèi)容,就說(shuō)明你成功了,注意的是上面地址的Users就是上面xml配置中的address的名稱(chēng),是一一對(duì)應(yīng)的。

    下面編寫(xiě)客戶(hù)端請(qǐng)求的代碼,代碼如下:

    package com.hoo.client;
     
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import com.hoo.entity.User;
    import com.hoo.service.IComplexUserService;
     
    /**
     * <b>function:</b>請(qǐng)求Spring整合CXF的WebService客戶(hù)端
     * @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) {
            //調(diào)用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);
        }
    }

    運(yùn)行后,可以在控制臺(tái)中看到

    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控制臺(tái)
     image 

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

    首先增加applicationContext-client.xml配置文件,文件內(nèi)容如下:

    <?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>

    客戶(hù)端請(qǐng)求代碼如下:

    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>請(qǐng)求Spring整合CXF的WebService客戶(hù)端
     * @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);
        }
    }

    運(yùn)行后結(jié)果如下:

    #############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
    本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。

     


    版權(quán)所有,轉(zhuǎn)載請(qǐng)注明出處 本文出自: http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html
    posted on 2012-08-21 11:17 abin 閱讀(1605) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): spring 、cxf
    主站蜘蛛池模板: 四虎在线播放免费永久视频| 亚洲人成无码网站久久99热国产| 免费a级毛片大学生免费观看 | 四虎影视永久免费观看地址| 国产日产亚洲系列| 亚洲一级免费视频| 国产成人精品免费大全| 国产高清不卡免费在线| 久久久久亚洲AV无码专区网站| 亚洲最大在线观看| 一级女人18片毛片免费视频| 波多野结衣免费在线观看| 亚洲熟妇少妇任你躁在线观看无码| 亚洲天堂一区在线| 一级白嫩美女毛片免费| 曰批全过程免费视频在线观看| 久久久久亚洲AV无码专区桃色| 亚洲色丰满少妇高潮18p| 成人网站免费看黄A站视频| 日韩精品视频免费观看| 婷婷亚洲综合五月天小说| 美女视频黄频a免费大全视频| 久久A级毛片免费观看| 亚洲天堂在线视频| 亚洲色大情网站www| 18禁成人网站免费观看 | 亚洲精品成人久久久| 亚洲三级在线播放| 久热免费在线视频| 亚洲国产精品无码久久久久久曰| 亚洲精品国产日韩无码AV永久免费网| 真人做人试看60分钟免费视频| 国产中文在线亚洲精品官网| 亚洲av无码一区二区三区天堂| 3d成人免费动漫在线观看| 中文字幕人成人乱码亚洲电影 | 亚洲人成网站在线播放vr| 国产精品自拍亚洲| 成年女性特黄午夜视频免费看| 久久水蜜桃亚洲av无码精品麻豆| 岛国精品一区免费视频在线观看|