Spring Boot or not!?
此文章自己翻譯自:https://www.baeldung.com/spring-vs-spring-boot
我覺得需要留下有Spring Boot開發跟純Spring開發Web App的差異
方便日後對照,因為設定上就差太多了
1.Overview
在本文詳細描述過程,我們將著重在標準Spring framework與Spring Boot的差異。
我們將專注討論Spring的模組,諸如MVC、Serurity,及核心Spring(Spring core)與Boot差別。
2.What is Spring?
簡言之,Spring framework提供Java開發者廣泛的基本架構的支援。
它提供一些不錯的功能如依賴注入(Dependency Injection)與箱外模組化如:
Spring JDBC
Spring MVC
Spring Serurity
Spring AOP
Spring ORM
Spring Test
這些模組可以大大降低應用程式的開發時間
例如早期Java Web開發時,我們需要寫一堆冗長的程式碼才能做到寫入資料進後端的datasource(如DB),但使用Spring JDBCTempplate模組,只要少少幾行程式碼與設定檔即可完成
3. What is Spring Boot?
Spring Boot只是擴展了Spring framework,透過建立Spring application省去了冗長的設定檔(configurations)
Spring Boot將使用一些固定的格式來鋪建更快更有效率地開發電子系統
如下是Spring Boot提供的一些功能
1.固定的'starter'依賴(dependency)來更簡單地組建應用設定
2.植入內嵌伺服器(容器)來避免複雜的系統佈置
衡量、運作生命確認、外部化設定
3.針對Spring各式套件自動化組建設置-只要有可能
讓我們一步步的熟悉這些framework
4. Maven Dependencies
首先在使用最基本的Spring來開發web系統所需要的Maven的dependency如下:
而改用Spring Boot只需要要一個dependency即可取得web應用程式執行所需的一切如下:
在建置過程所有需要使用到的dependencys將自動的由該Spring Boot的dependency(如上這單一個)加入
(意即spring-boot-starter-web已包含spring-web與spring-webmvc)
其他好的例子諸如測試的libraries,通常我們會用到一堆如Spring Test、JUnit、Hamcrest、Mockito libraries。我們要在Spring專案內使用這些套件,就需要一個個引用各自的dependency。
但是若改用Spring Boot,我們只需要使用一個starter dependency即會自動包含所有測試用的libraries內容
Spring Boot針對不同Spring模組提供許多starter dependencies
常見的有:
spring-boot-starter-data-jpa
spring-boot-starter-serurity
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
要更多其他starter請見Spring documentation
5. MVC Configuration
當使用Spring及Spring Boot開發JSP應用web程式有各自的 configuration(設定定義檔)需求
一般Spring需要定義 dispatcher servlet , mappings, 及其他支援的configurations(設定定義檔)。我們可以透過web.xml或初始化( Initializer)的class檔來定義 範例如下
我們甚至需要加入 @EnableWebMvc annotation給 @Configuration class並且定義 view-resolver去解析來自controller的view定義
而在Spring Boot來說只需要寫幾行properties檔內定義即可
在有 web starter情況下只要加入如下設定的properties定義檔
在有Spring Boot web starter情況下,透過圖2設定,將自動配置如程式碼1設定的內容,我們稱為auto-configuration(自動佈置)
這代表Spring Boot將自己尋找在應用程式內存在的dependencies與properties及bean,並依其定義將其啟用
當然若我們有需要加入客製化的設定定義,那麼Spring Boot的auto-configuration(自動佈置)將 back away
5.1. Configuring Template Engine
現在當我們嘗試設置一個 Thymeleaf template engine在Spring與Spring Boot專案當中
在Spring專案內我們為了定義 view resolver需要加入 thymeleaf-spring5 dependency和一些設定定義檔如下
而在Spring Boot 1的Web應用程式,只需要 spring-boot-starter-thymeleaf這個dependency就可以啟用 Thymeleaf相關的支援,然而在Spring Boot 2的Web應用程式下Thymeleaf 3.0版我們還另外需要加入 thymeleaf-layout-dialect這個dependency
當一切dependency都準備洽當時,我們就可以在 src/main/resources/templates目錄加入相關的 templates,並且Spring Boot將會自動佈置這些 templates
6. Spring Security Configuration
為了一些明確的目的,我們將展示初始如何透過這些framework啟用HTTP基本身份驗證
我們從dependency與 configuration(設定檔定義)開始著手,我們需要透過Spring來啟用Serurity機制
Spring需要兩個標準的dependencies才能在應用程式啟用Serurity機制,1. spring-security-web 2. spring-security-config
接著我們需要建一個class來extends(拓展) WebSecurityConfigurerAdapter並使用@ EnableWebSecurity這個annotation
這裡我們將使用的是記憶體驗證( inMemoryAuthentication)來啟用驗證機制
相同地Spring Boot也需要這些dependencies來運作,但是我們只需要定義一個dependency叫 spring-boot-starter-security,該dependency就已經包含所有需要的內容
而Spring Boot的 security設定定義檔與上面的java檔相同
如果你想瞭解關於JPA在Spring與Spring Boot間相關設定差異可以詳見此文章A Guide to JPA with Spring
7. Application Bootstrap
Spring 與 Spring Boot在驅動應用程式的一個差異是Servlet,Spring仍需透過web.xml或 SpringServletContainerInitializer來當做驅動的起點
而Spring Boot使用Servlet 3的功能來驅動應用程式,讓我們來說說細項
7.1. How Spring Bootstraps?
Spring 不僅支援web.xml來做驅動,也支援Servlet 3方法來驅動
讓我們來一步步解析web.xml做的事:
- Servlet 容器(the server 可能是tomcat或其他)讀取web.xml
- 在web.xml中定義的DispatcherServlet將由容器產生實例
- DispatcherServlet透過讀取 WEB-INF/{servletName}-servlet.xml建立 WebApplicationContext
- 最後 DispatcherServlet註冊一切定義於應用程式context的beans
接著一步步解析Spring 如何透過Servlet 3驅動應用程式
- 容器搜尋classes有 implementing ServletContainerInitializer並執行
- SpringServletContainerInitializer找出所有classes有 implementing WebApplicationInitializer
- WebApplicationInitializer透過XML或 @Configuration classes建立context
- WebApplicationInitializer透過前面建立的context來建出 DispatcherServlet
7.2. How Spring Boot Bootstraps?
Spring Boot應用程式的實體化進入點是有@ SpringBootApplication這個annotation的class 範例如下
預設情況下Spring Boot使用內建的容器來執行應用程式,如上程式碼下,Spring Boot使用 public static void main來當做程式用行的起點,以驅動web server
並且它也負責為內建的 servlet容器綁定源自應用程式的context定義的Servlet、Filter、 ServletContextInitializer bean
另一個Spring Boot的功能是自動掃瞄在這個Main-class下所有在同層package或子package下所有的class內的元件(components)
Spring Boot也可以佈署在外部容器
在這個例子裡,我們必須 extend(拓展) SpringBootServletInitializer 範例如下
在外部Servlet容器會找到定義在web檔案內META-INF目錄下的Main-class當程式運行進入點,而該進入點class因有拓展 SpringBootServletInitializer將會同時綁定Servlet、Filter、 ServletContextInitializer
8. Packaging and Deployment
最後來討論應用程式如何包裹與佈署,這一切framework皆支援一般包裹管理技術如Maven或Gradle,但是當談到佈署就有許多不同了
舉例 Spring Boot Maven Plugin提供Spring Boot對Maven的支援,並且也提供可直接執行的包裹如jar(即runnable jar)或war
並且運行在應用程式內
一些佈署上,Spring Boot比純Spring優的地方如下:
- 提供內嵌容器
- 透過cmd執行 java -jar命令句,可直接執行該jar檔
- 避免在佈署過程中產生一些dependency套件衝突
- 部署時提供選擇配置文件的選項(比如說你jdbc連線資訊IP會依測試正式不同,所以定義在測試的配置文件與正式的配置文件中,隨時依你所選定來配置)
- 隨機產生的port來配合集成測試
9. Conclusion
在這個教學文件中,我們學習關於Spring與Spring Boot的差異
在短短的文字描述下,我們可以說Spring Boot是簡單地更加方便的拓展了Spring本身在開發、測試、部署