介紹 Spring framework 相關,核心 -> IOC/DI/AOP
Spring: 簡化Java開發
- IOC: 耦合管理(兩個模組間的關連性或相依性)
- AOP: 可重用的功能,不影響原服務邏輯
Spring Boot: 簡化 Spring 應用開發
自動配置應用程式及框架所需的基本配置(如 properties 相關)- 嵌入式 Servlet,不用打包成war檔
- 簡化 Maven 配置
- 參考 Spring 常用註解
Ioc/DI
DI實現Ioc
- 不用自行new 實例,透過Spring注入稱DI -> 整個行為代表Ioc
(new 的控制權從我們反轉至Spring)
AOP: 動態代理、使用@Transactional
@Transactional: 常用在類別或方法上,遇到異常則 Rollback
Spring MVC
Model: 進行資料管理和資料庫設計
View: 前端網頁的資料呈現
Controller: 處理請求
邏輯處理程式架構
Controller -> ApiService(Data preproccess、ApiResponse) -> Service(interface) -> ServiceImplement(SQL、Hibernate)
Design Pattern
BeanFactory: @Component (工廠模式),用來建立物件的實例Singleton: 檢查是否存在實例,若無則鎖住並處理 (Bean)HandlerAdatper: 每一種Controller有一對應的HandlerAdatper Implement,讓其代替controller執行相對應的方法。擴展Controller上,僅需增加HandlerAdatper即完成SpringMVC之擴展。Wrapper & Decorator: 連接多個資料庫可使用,動態切換不同資料庫AOP: 程式的前或後(或前後)執行其他的程式,不影響到原本的業務邏輯 (交易過程中的身分驗證)Observer: 物件中有依賴關係,若其一狀態更動,其他物件會收到通知,被動更新,使用ListenerStrategy: 使用Resource (針對不同的的底層資源,提供方法使用)Template: 解決重複程式碼問題,Hibernate、RestTemplate
Spring & Spring Boot Diff
Spring
provides comprehensive infrastructure support for developing Java applications.
(Modules)
- Spring JDBC
- Spring MVC
- Spring Security
- Spring AOP
- Spring ORM
- Spring Test
Spring Boot (Extends spring)
Embadded server, Automatic config
- spring-boot-starter-data-jpa
- spring-boot-starter-security
- spring-boot-starter-test
- spring-boot-starter-web
- spring-boot-starter-thymeleaf