天天看點

apache cxf筆記之利用spring建立服務程式

前面我們已經介紹了簡單的jax-ws服務程式,關于SEI接口,接口的實作還有服務的釋出。這邊将介紹另外一種服務的釋出形式——基于spring容器。

其中SEI接口和接口實作類代碼不改變,參見上一次的apachecxf筆記。

在原有的項目的src目錄先建立server-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/schemas/jaxws.xsd">
	
	<jaxws:endpoint
		id="ProjectManager" implementor="demo.cxf.helloworld.HelloWorldImpl" 
		address="http://localhost:8080/HelloWorld"/>

</beans>
           

編寫測試代碼:

SpringServer類:

package demo.cxf.helloworld.server;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringServer {

	public static void main(String[] args) throws IOException {
		SpringServer springServer = new SpringServer();
		springServer.startServer();
		System.out.println("Startting ready...");
		System.in.read();//按任意鍵退出
		System.out.println("Server exiting...");
		System.exit(0);//程式退出
	}
	
	public void startServer(){
		System.out.println("Starting Server.....");
		
		//通過spring容器讀取服務配置資訊,建立服務
		new ClassPathXmlApplicationContext(new String[]{"classpath:server-beans.xml"});
	}
}
           

運作,控制台為:

apache cxf筆記之利用spring建立服務程式

浏覽器通路:http://localhost:8080/HelloWorld?wsdl

apache cxf筆記之利用spring建立服務程式