上一節介紹了如何使用CXF 來釋出服務,但是沒有介紹使用web 容器來釋出,很多項目需要用tomcat 這樣的容器來釋出。另外本節将介紹CXF 與spring 結合的方法。
一 目标:
1、利用sping的ioc特性,進行釋出服務對象的配置
2、通過tomcat 釋出服務
二、環境準備
1、利用上一節的工程源碼,做少量調整
2、myeclipse2015
3、使用spring 3.0 和cxf 2.7.6
三、實踐步驟
1、建立web project 工程
注意裡面需要勾選的地方
建議一路next 下去,而不是直接finish,在最後一頁,把web.xml 勾選上,再finish
2、建立工程後,前面2節中涉及的java 類都copy 到目前工程裡面,同時建立lib庫,将相關的lib 添加到build path
源碼結構如下圖:
添加依賴的lib,結構如下
修改源碼 StudentWsImpl.java 類,主要是去掉main方法中内容,因為現在的釋出在tomcat容器中
package com.study.cxfws.impl;
import javax.jws.WebService;
import com.study.cxfws.StudentWs;
import com.study.dao.StudentDAO;
import com.study.dao.impl.StudentDAOImpl;
@WebService
public class StudentWsImpl implements StudentWs {
//Student的dao 類,負責處理student 實體類的操作
private StudentDAO studentDAO;
public StudentWsImpl(){
studentDAO = new StudentDAOImpl();
}
public boolean addStudent(String name, String sex, String birthday) {
// 調用studentDAO.addStudent 方法入庫
System.out.println("Now put student into DB!");
studentDAO.addStudent(name, sex, birthday);
return true;
}
public String queryStudent(String studentName) {
System.out.println("StudentWsImpl queryStudent->"+studentName);
Object tmp = studentDAO.queryStudent(studentName);
if (tmp== null) {
return "null";
} else {
return tmp.toString();
}
}
public static void main(String[] args) {
}
}
3、 在src 目錄下,配置sping 的配置檔案
在src 目錄,建立一個sping的配置檔案applicationContext-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" />
<bean id="StudentWsImpl" class="com.study.cxfws.impl.StudentWsImpl" />
<jaxws:server id="StudentSpingWs" serviceClass="com.study.cxfws.StudentWs"
address="/StudentSpingWs">
<jaxws:serviceBean>
<ref bean="StudentWsImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
這裡xml 配置檔案裡面, bean id 指定了web service 接口實作類,也就是調用接口後實際業務邏輯的實作類; server id 和address 是 web service接口暴露的位址,對應的是接口類 StudentWs, 而這個服務的serviceBean 指定為接口實作類,這樣就指定了運作時調用的實作類。 這個配置也就是sping核心的ioc控制反轉功能,通過配置檔案将依賴的對象,用配置檔案來實作依賴注入,進而實作控制反轉。
4、修改web.xml 檔案
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StudyWsSpringCXF</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-server.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
context-param裡面指定了spring 配置檔案
5、釋出到tomcat裡面
在myeclipse裡面,找到server,右鍵 tomcat 7.0
選擇Add/Remove Deployments 後,在彈出對話框裡面,選擇本工程進行釋出
6、 運作tomcat
運作後,如果在console 視窗,出現下面内容,說明tomcat 已經運作起來
二月 04, 2017 11:24:24 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
二月 04, 2017 11:24:24 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
二月 04, 2017 11:24:24 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 14766 ms
7、檢視釋出的服務
通過下面url ,來檢視釋出的服務:http://localhost:8080/StudyWsSpringCXF/services 這裡StudyWsSpringCXF是工程項目的名稱,如果一切正常,打開的界面如下:
可以點選紅框中内容擷取wsdl 文檔。
四、通過用戶端驗證
運作之前的用戶端,驗證一下釋出的服務端能正常工作,需要注意的是,需要修改用戶端類StudentWsClient.java裡面調用服務端的位址,與目前釋出的服務端一緻
factory.setAddress("http://localhost:8080/StudyWsSpringCXF/services/StudentSpingWs");
五、總結
通過sping+tomcat+cxf 釋出webservice 服務的步驟如下:
1、建立工程、編寫web service 服務端相應的代碼,實作業務邏輯
2、編寫sping的配置檔案 applicationContext.xml 檔案,這個檔案放在src 的根目錄下
3、編寫配置web.xml 檔案
4、釋出到tomcat中、并進行調試
附:在本機環境下,執行10000次查詢,關閉掉system列印,耗時:run time=37410 毫秒,平均每秒268次