CXF Spring統合――もう一つのハローワーク!

6684 ワード

1、アプリにcxfの最新jarパッケージをダウンロードします。  私のは2.5.2です
住所:http://www.apache.org/dyn/closer.cgi?path=/cxf/2.5.2/apache-cxf-2.5.2.zip
2、webプロジェクトを新規作成し、Springを整合する
プロジェクト名、MyEclipse->Add Spring Capabilitiesを右クリックします。
Spring versionをSpring 2.5として選択し、Spring 2.5 AOP Libries、Spring 2.5 Core Libries、Spring 2.5 Persistens Core Libries、Spring 2.5 Persistens JDBC Libries、Spring 2.5 Web Libriesの5つのバッグを選択します。Copy checked Library content to project folderを選択して、Nextをクリックします。
Nextをクリックして、Folderテキストボックスをクリックした後のBrowseをクリックして、ポップアップダイアログの中でspring配置ファイルの保存位置を選んでWebRoot/WEB-INFで、Finishをクリックします。ここでSpringのロードが完了しました。WEB-INFフォルダの下でプロファイルappication Contact.xmlが生成されました。
3、apache-cxf-2.5.2包を解凍し、すべてのjarをクラスパスにロードする。
4、まずweb.xmlに下記の構成を追加します。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!--   Spring     -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--   Spring           -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext-*.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>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

5、srcディレクトリにおいて、新しいappication Controtext-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" />

</beans>
6、次はサーバーのエンドコードを書き始めます。まずサーバ側のインターフェースをカスタマイズします。コードは以下の通りです。

package test;

import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	public String sayHello(String name);
}
次はWebServiceの実現クラスを作成します。サーバー側のコードは以下の通りです。

package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements IHelloWorld {

	
	public String sayHello(String name) {
		System.out.println("sayHello is called by " + name);
		return "Hello " + name;
	}

}

Springと統合して、ここで必ずインターフェースを完成します。インターフェースがないと間違いがあります。
以下の構成をappication Contect-server.xmlファイルに追加します。

<jaxws:endpoint 
	    id="helloWorld" 
	    implementor="test.HelloWorldImpl" 
	    address="/HelloWorld" />
以下でtomcatサーバを起動したら、WebBrowserで要求します。
http://localhost:8088/SnipeCXFServer/HelloWorld?wsdl
wsdlのxmlファイルの内容を見ることができれば、成功したと説明します。上の住所のハローワールドは上のxml配置の中のaddressの名前です。一つずつ対応しています。
7、まずappication Contront-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="helloWorldClient" 
		address="http://localhost:8088/SnipeCXFServer/HelloWorld" 
		serviceClass="test.IHelloWorld"/>
</beans>
8、以下にクライアント要求のコードを作成します。コードは以下の通りです。

package client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.IHelloWorld;

public class Client {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
		IHelloWorld helloWorld = (IHelloWorld) context.getBean("helloWorldClient");
		System.out.println(helloWorld.sayHello("Test"));
	}

}
運転後の結果は以下の通りです。
2012-3-9 15:48 org.apache.cxf.service.factory.Reflection ServiceFactoryBern buildServiceFroomClass
情報:Creating Service{http://test/}IHello World Service froomclastest.IHello World
ハローTest