我們很高興地宣布,Spring Petclinic 範例應用程式已重構。
原始程式碼現在
可在 github 上取得。這是新應用程式的螢幕截圖:

這是新架構的概觀
Spring, Spring, Spring
我們使用了以下 Spring 功能
- 使用 jdbc (JdbcTemplate)、JPA 或 Spring Data JPA(儲存庫層)進行資料存取整合。您可以透過在 web.xml 或其中一個 JUnit 測試中設定相應的 Bean 配置文件來選擇要使用的實作。
- 使用 @Transactional 的交易(服務層)
- 使用 @Cacheable 與 ehcache 作為快取實作的快取(服務層)
- Spring MVC:使用 Bean 驗證 (JSR-303) 進行表單驗證
- Spring MVC:使用 ContentNegotiatingViewResolver 進行內容協商(html、xml 或 atom)。
- Spring MVC:使用 SimpleMappingExceptionResolver 進行例外處理
- Spring MVC:使用 Spring MVC 測試框架
我們充滿活力的社群
我們獲得了社群專家的相當多的貢獻(包括幾個開源專案的負責人)。
Thymeleaf
如果您還沒聽過 Thymeleaf,它可以被視為 JSP 的替代品。它將自身定義為 XML / XHTML / HTML5 模板引擎。
它基於一些純 HTML 檔案,帶有一點命名空間魔法。
Thymeleaf 專案的 Daniel 和 Soraya 創建了一個使用 Thymeleaf 代替 JSP 的 Spring-Petclinic 分支。他們在這篇部落格文章中記錄了遷移步驟:http://www.thymeleaf.org/petclinic.html
Spring Petclinic 的 Thymeleaf 分支可在此處取得:https://github.com/thymeleaf/thymeleafexamples-petclinic
若要深入了解 Thymeleaf:http://www.thymeleaf.org
在 Twitter 上關注 Thymeleaf:https://twitter.com/thymeleaf
蒲公英 (Dandelion)
Dandelion 提供了一組可以與 JSP 或 Thymeleaf 一起使用的標籤庫。
我們已在 Spring-Petclinic 中使用它來處理 DataTables。它基於 jQuery DataTables 和 Bootstrap 生成表格。
您可以執行以下操作
<datatables:table data="${ownerList}" id="dataTable" theme="bootstrap2" export="pdf">
<datatables:column title="Name" property="name" sortable="true" />
<datatables:column title="Address" property="address" sortable="true" />
</datatables:table>
然後輸出 html 表格如下所示
Dandelion 在 Spring Petclinic 的主要分支中使用。
Dandelion 專案的 Thibault Duchateau 撰寫了一篇不錯的部落格文章,描述了 Spring Petclinic 應用程式的遷移
若要深入了解 Dandelion:
http://dandelion.github.com/在 Twitter 上關注他們:https://twitter.com/dandelion_proj
Maven 或 Gradle?
預設情況下,Spring Petclinic 使用 Maven,因為它是 Java 應用程式最常見的選擇。雖然與大多數實際應用程式相比,Spring Petclinic 相當小,但其 Maven pom.xml 檔案已經非常冗長。
Thoughtworks China 的 Li Yanhui 非常友善地將 Spring Petclinic 遷移到 Gradle。這是並排比較 Maven 和 Gradle 的絕佳方式。build.gradle 配置文件確實更容易理解。它目前包含 143 行(而 Maven POM 則為 543 行)。
您可以在此處瀏覽基於 Gradle 的 Spring Petclinic 版本:
https://github.com/whimet/spring-petclinic 感謝他們,我們能夠識別 Spring Petclinic 中包含程式碼重複且缺乏自動化測試的一些位置。
效能測試:讓我們擴展!
是否有可能採用現在的 Spring Petclinic,並將其擴展到單個伺服器實例上的每秒 1000 個請求?Ippon Technologies 的 Julien Dubois 撰寫了關於該主題的五篇部落格文章的精彩系列。
它回答了以下問題:- 我應該依賴會話上下文嗎?(第 2 部分) - 我應該使用哪個 Apache Tomcat 連接器?(第 2 部分) - 我應該使用哪個資料庫連線池?(第 3 部分) - JDBC 比 JPA 或 Spring Data JPA 更快嗎?(第 4 部分) - 使用 OpenSessionInViewFilter 的優點是什麼?(第 4 部分)
參考文獻
GitHub 上的 Spring Petclinic Cloud Foundry 上的 Spring Petclinic Petclinic + Thymeleaf Petclinic + Gradle