Apache CXF 整合Spring

1、建立一個 Java Web 工程,目錄最終的結構以下圖,下面咱們將遂一說明: java

2、把咱們要用到的jar包所有放到lib目錄下。 web

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


<?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、建立服務器端代碼: apache

    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: session

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

<?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 放進去ide

    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接口了 ui

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



5、建立客戶端,該類的實現比較簡單,就是獲得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);
	}
}



6、部署到Tomcat:


7、運行Client 類並打印: url

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
相關文章
相關標籤/搜索