2026年9月11日 星期五

Spring Boot:建置與執行

 


在漫長的 Java 語言開發平台發展路途中,自從由官方主導的 Java EE 被開源社群擁護的 Spring 攻城掠地後,長期不動如山的 Servlet 網頁程式開發技術,也逐步被 Spring MVC 取代。然而,Spring 框架越來越龐大的類別庫相依性,卻成為 Java 工程師建置開發環境時的夢魘。Spring 開發團隊敏銳地發現,如此龐大的開發成本將會阻礙框架的普及,因此推出了 Spring Boot。其簡化的依賴性管理、簡化的部署以及自動組態三大特性,讓 Spring Boot 重新成為 Java 開發者喜愛的程式框架。

其實之前就已經看過兩本 Spring Boot 的專書,也已經將手上的專案全數改成 Spring Boot 框架。因此,閱讀這本《Spring Boot:建置與執行》的原意,只是單純想湊齊看完市面上這一主題的書。特別是由O'REILLY出版的電腦叢書,早期在 Java 領域裡幾乎等同於官方教材般的尊榮地位。雖然隨著網路興起,類似資訊隨手可得,讓系列叢書逐漸變得乏人問津,不過這兩年開始發現,碁峯和O'REILLY聯手出版的新書,有著充實的技術說明,輔以實用的程式範例,相當類似我最欣賞的Manning出版社In Action系列書籍。而在看完這本《Spring Boot:建置與執行》後,一方面吸收到之前兩本書中所沒有的內容,另一方面也重新溫習了已經學過的技術。

書中從最基礎的Spring Initializr專案建置工具講起,先用最單純的 Hello World 程度 REST API 程式,向讀者展示 Spring Boot 是如何簡化網頁程式開發的複雜度。接著再話說從頭,細數應用程式組態在 Spring Boot 框架下的規則,以及如何靈活運用。有如JMX 的致動器(Actuator)便是這本書獨有的章節;其他像是如何打包、部署成容器,也都是其他書裡沒提過的內容。和資料操作有關的 Spring Data 幾乎是必講的重頭戲,書中一次展示如何連接關聯式資料庫、MongoDB、Redis 三大儲存體,讓讀者不論挑選哪一種儲存技術,都可以找到參考範例。

全書我覺得最值回票價的部分,是學到了 Spring WebFlux 的反應式網頁程式(Reactive Programming)技術,以及之前一直沒有實作的 WebSocket 應用。也因此茅塞頓開地弄懂了 React、Reactive、WebSocket 三種常同時被提及,卻又截然不同、用來改善網路程式瓶頸的技術。甚至讓我破除了單一主機 TCP 連線只能到 65,535 條的迷思,可說是收穫滿滿,開卷有益莫過於此。


Throughout the long history of Java development platforms, Java EE was once the official choice for Java developers. However, after Spring, supported by the open-source community, gradually became more popular, the long-standing Servlet-based web development technology was also gradually replaced by Spring MVC. However, the growing number of library dependencies in the Spring Framework became a nightmare for Java engineers when setting up their development environments. The Spring development team quickly realized that such a high development cost could prevent the framework from becoming more popular. So, they introduced Spring Boot. With three major features — simplified dependency management, simplified deployment, and auto-configuration — Spring Boot once again became one of the most popular frameworks among Java developers.

Actually, I had already read two books about Spring Boot and had also migrated all of my current projects to the Spring Boot framework. Therefore, my original reason for reading *Spring Boot: Build and Run* was simply to complete my reading of the books available on this topic. In particular, computer books published by O'Reilly once had an almost official-textbook-like status in the Java field. However, as the Internet became more popular and similar information became easily available online, these book series gradually became less popular. Over the past two years, however, I have noticed that the new books jointly published by CITE Publishing and O'Reilly provide detailed technical explanations along with practical programming examples. They are quite similar to the *In Action* series published by Manning, which is one of my favorite book series. After finishing *Spring Boot: Build and Run*, I learned some new things that were not covered in the previous two books. At the same time, I also reviewed some technologies that I had already learned.

The book starts with the basic project creation tool, Spring Initializr. It first uses a very simple Hello World-level REST API example to show readers how Spring Boot can simplify the complexity of web application development. The book then goes back to the basics and explains the rules of application configuration in Spring Boot and how to use them flexibly. The Actuator likes JMX is one of the topics that I had not seen in the other books. Other topics, such as how to package an application and deploy it as a container, are also not covered in the other books I have read. Spring Data, which is related to data access, is naturally one of the major topics in the book. It demonstrates how to connect to three major types of data storage: relational databases, MongoDB, and Redis. No matter which storage technology readers choose, they can find useful examples in the book.

The part of the book that I found most valuable was learning about reactive web application development with Spring WebFlux and finally getting some hands-on experience with WebSocket, which I had never implemented before. Because of this, I finally understood the differences between React, Reactive, and WebSocket. These three terms are often mentioned together, but they are actually completely different technologies that can be used to address different kinds of problems in web applications. The book even helped me get rid of my old misunderstanding that a single host can only have 65,535 TCP connections. I learned a lot from this book. This is probably the best example of how reading a good technical book can be truly useful.


沒有留言:

張貼留言