Apache CXF 整合Spring

jopen 10年前發布 | 65K 次閱讀 WEB服務/RPC/SOA Apache CXF

本例是Apache CXF 整合Spring,并將工程服務發布到 Tomcat 的一個例子,主要是參考官方的實現方式。實現2個WebService方法,一個是返回String類型的,一個是返回List類型方式的。 

一、創建一個 Java Web 工程,目錄最終的結構如下圖,下面我們將遂一說明:

Apache CXF 整合Spring

二、把我們要用到的jar包全部放到lib目錄下。

三、修改web.xml文件,整合CXF。


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>HelloSpringCXF</display-name>
    <servlet>
        <description>HelloSpringCXF</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>/services/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>




四、創建服務器端代碼:

    4.1 接口HelloWorld 會用到一個@WebService注釋,說明是WebService接口:


package com.yao.spring.service;

import java.util.List;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHi(String text);

    List<String> getListString();
}


    4.2 實現接口,HelloWorldImpl其中有一個方法是返回List的,CXF 可以完美支持 List,類有個注釋 @WebService ,并有一個屬性 endpointInterface ,用于告訴調用所實現的接口 :



package com.yao.spring.service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

@WebService(endpointInterface = "com.yao.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }

    @Override
    public List<String> getListString() {
        List<String> lists = new ArrayList<String>();
        lists.add("AA");
        lists.add("BB");
        lists.add("CC");
        return lists;
    }


}

    3.3 在 WEB-INF 下 創建 cxf-servlet.xml  文件,整合Spring:

Apache CXF 整合Spring

    4.4 cxf-servlet.xml 文件的內容如下,jaxws:endpoint 填寫要實現的類,address為地址這個要對應下面提到的4.6 里面 spring配置文件client-beans.xml的address地址,要填相對路徑:

<?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/schemas/jaxws.xsd">
    <jaxws:endpoint id="helloWorld" implementor="com.yao.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>

    4.5 在根文件夾下創建client-beans.xml 文件,這個為spring的配置文件。本例中創建了一個源文件夾config,并把client-beans.xml 放進去

Apache CXF 整合Spring

    4.6 client-beans.xml 文件如下,<property name="address" value="http://localhost:8080/HelloSpringCXF/services/HelloWorld"/>這個為設置部署到Tomcat后WebService的訪問地址,最后的/HelloWorld 就是對應 4.4 cxf-servlet.xml 里jaxws:endpointaddress,這個client bean 就可以調用服務器里的HelloWorld接口了

<?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="com.yao.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.yao.spring.service.HelloWorld"/>
        <property name="address" value="http://localhost:8080/HelloSpringCXF/services/HelloWorld"/>
    </bean>
</beans>



五、創建客戶端,該類的實現比較簡單,就是得到spring里的bean client并調用其方法:

package com.yao.spring.client;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yao.spring.service.HelloWorld;


public final class Client {

    private Client() {
    }

    public static void main(String args[]) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" });

        HelloWorld client = (HelloWorld) context.getBean("client");

        String response = client.sayHi("Joe");
        System.out.println("Response: " + response);

        List<String> lists = client.getListString();
        if (lists != null && lists.size() > 0) {
            for (String list : lists) {
                System.out.println(list);
            }
        }

        context.close();
        System.exit(0);
    }
}



六、部署到Tomcat:

Apache CXF 整合Spring


七、運行Client 類并打印:

2014-8-20 17:32:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@735cda3f: startup date [Wed Aug 20 17:32:09 CST 2014]; root of context hierarchy
2014-8-20 17:32:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [client-beans.xml]
2014-8-20 17:32:09 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5d6d2633: defining beans [client,clientFactory]; root of factory hierarchy
2014-8-20 17:32:09 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.spring.yao.com/}HelloWorldService from class com.yao.spring.service.HelloWorld
Response: Hello Joe
AA
BB
CC
2014-8-20 17:32:10 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@735cda3f: startup date [Wed Aug 20 17:32:09 CST 2014]; root of context hierarchy
2014-8-20 17:32:10 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5d6d2633: defining beans [client,clientFactory]; root of factory hierarchy


來自:http://my.oschina.net/jamaly/blog/304843

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!