Spring與CXF

来源:http://www.cnblogs.com/starlin/archive/2016/03/01/5230822.html
-Advertisement-
Play Games

最近項目中有用到Webservice,研究了Spring與CXF整合的官方文檔(其實官方文檔說的清楚了,建議大伙還是看官方文檔,都是很簡單英文單詞),所以就有了下麵的demo,相信大伙都能看懂 用的主要架構為Maven + Spring + CXF + IDEA,廢話就不多說了,先看下整個個項目結構...


            最近項目中有用到Webservice,研究了Spring與CXF整合的官方文檔(其實官方文檔說的清楚了,建議大伙還是看官方文檔,都是很簡單英文單詞),所以就有了下麵的demo,相信大伙都能看懂

            用的主要架構為Maven + Spring + CXF + IDEA,廢話就不多說了,先看下整個個項目結構

一、項目結構圖

      image

二、服務端

   1.我們首先來編寫服務端介面HelloWorld.java

package cn.starlin.springcxf;

import javax.jws.WebService;

/**
 * 定義服務介面
 * on 2016/03/01 9:34.
 */

@WebService
public interface HelloWorld {
    public String sayHello(String str);
}

  2.定義服務端介面實現類HelloWorldImpl

package cn.starlin.springcxf;

import javax.jws.WebService;

/**
 * 服務介面實現類
 * on 2016/03/01 9:36.
 */
@WebService(endpointInterface = "cn.starlin.springcxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String str) {
        System.out.println("syaHello called");
        return "Hello " + str;
    }
}

  3.聲明servers的bean,創建cxf-servlet.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!--由Spring管理-->
    <bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" />

    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>

  4.配置web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>cxf</display-name>

  <servlet>
    <description>Apache CXF Endpoint</description>
    <display-name>cxf</display-name>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>

</web-app>

  5.啟動Tomcat,訪問http://localhost:8080/SpringCXF/,若能出現下圖界面,就表示服務端成功了

image

 

三、客戶端

  1.客戶端 client-beans.xml

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

    <bean id="client" class="cn.starlin.springcxf.HelloWorld"
          factory-bean="clientFactory" factory-method="create"/>

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/>
        <property name="address" value="http://localhost:8080/SpringCXF/HelloWorld"/>
    </bean>
</beans>

  2.客戶端代碼HelloWorldClien.java

package cn.starlin.springcxf;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客戶端
 * on 2016/03/01 10:53.
 */
public class HelloWorldClient {
    public static void main(String args[]) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
        HelloWorld client = (HelloWorld)context.getBean("client");//client需與client-beans.xml中的id一致
        String response = client.sayHello("starlin");
        System.out.println("Response: " + response);
        System.exit(0);

    }
}

  3.運行以下客戶端代碼,出現以下信息就表示成功了

image

同時服務端也會列印出相關信息

image

 

 

至此整合完成!!!!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 百度一下”asp.net身份認證“,你會得到很多相關的資料,這些資料通常上來就會介紹諸如”Form認證“”Windows認證“等內容,而沒有給出一個完整的流程。初學者對此往往一頭霧水,我也曾經被坑過很多回,於是寫下此文,算是複習。 現代的Windows Server系統都是基於嚴格的用戶機制的,當你
  • 好了,還是這張圖,還是一樣的Hello world。 因為本章其實很多都是講一些命令行編譯啊什麼鬼的配置類的東西,要用的時候直接百度或者回頭查書就可以了, 所以瞭解一下也就行了,也沒有記錄下來,接下來講得只是我認為很有用的東西。 關於引用 請看上圖,MyTest程式集下麵有個引用,引用裡面大家都知道
  • 上次業務邏輯和展示層的架構都寫了,可以開始進行具體功能的實現,這次先實現管理員的登錄、驗證和註銷功能。 一、業務邏輯層 1、實現256散列加密方法。 Ninesky.Core【右鍵】-> 添加->文件夾,輸入文件夾名General。 General文件夾【右鍵】->添加->類,輸入類名Securit...
  • 一. 在調試時,不要使用調試程式的X號關掉程式,而是要用VS自帶的停止調試的介面,即那個小方塊。不然下次調試會出現異常,若真出現這種情況,可以右鍵項目名,點擊清理。 二. Visible屬性 是“可見”的意思,若在屬性里設置 this.visible=false;會使當前控制項隱藏, 若在控制條件里設
  • MVVMlight在UWP開發中的使用
  • 在ASP.NET2.0開始,提供了母版頁的功能。母版頁由一個母版頁和多個內容頁構成。母版頁的主要功能是為ASP.NET應用程式中的頁面創建相同的佈局和界面風格。母版頁的使用與普通頁面類似,可以在其中放置文件或者圖形、任何HTML控制項和Web控制項、後置代碼等。 母版頁僅僅是一個頁面模板,單獨的母版頁是
  • 繼續,前面已經實現了C#調用Windows API實現了彈出對話框功能。使用了User32.dll文件,主要代碼如下:[DllImport("User32.dll")]public static extern int MessageBox(int h, string m, string c, int
  • 以下為本次實踐代碼: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Linq; 5 using System.Reflection; 6 using
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...