前面我們已經介紹了簡單的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"});
}
}
運作,控制台為:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICdzFWRoRXdvN1LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90TUOFzZq5Ee0IjY6VzVaZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zM1kDNxcjMyIDMxcDMzEDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
浏覽器通路:http://localhost:8080/HelloWorld?wsdl