SpringBoot 2.0學習總覽

一、總覽

SpringBoot 1.0是基於Servlet + JavaEE + springMVC

SpringBoot 2.0是基於Reactive + Netty,Servlet + Spring WebFlux, 非阻塞異步的方式

- 組件自動裝配:模式註解、@Enable註解、條件裝配、加載機制

- 外部化配置:Environment抽象、生命週期、破壞性變更

- 嵌入式容器:Servlet Web容器、Reactive Web容器(Netty)

- Spring Boot Starter:依賴管理、裝配條件、裝配順序

- Production-Ready:健康檢查、數據指標、@Endpoint管控

二、SpringBoot 3大核心特性

1、組件自動裝配:Web MVC、Web Flux、JDBC等

  • 激活:@EnableAutoConfigration
  • 配置:/META-INFO/spring.factories(源信息的目錄)
  • 實現:XXXAutoConfigration

2、嵌入式Web容器:

  • Web Servlet:Tomcat、Jetty等
  • Web Reactive:Netty Web Server

3、生產準備的特性:

  • 指標:/actuator/metrics,記錄CPU、內存、磁盤等信息(web和JMX兩種方式)
  • 健康檢查:/actuator/health
  • 外部化配置:/actuator/configprops

三、Web應用

1、傳統Servlet應用:

  • Servlet組件:Servlet、Filter、Listener
  • Servlet註冊:Servlet註解、Spring Bean、RegistrationBean

Servlet實現:@WebServlet,extends HTTPServelet(重寫doGet、doPost)

URL映射:@WebServlet(urlPatterns="/myservlet")

註冊:@ServeltComponentScan="com.xxx.web.servelet" ,Servlet所在包路徑掃描

  • 異步非阻塞:異步Servlet、非阻塞Servlet
類註解開啓異步支持:@WebServlet(urlPatterns="/myservlet,asyncSupported = true")
開啓異步:AsyncContext asyncContext = req.startAsync(); // 異步的上下文
執行異步邏輯:asyncContext.start(()->{ Runnable() });
觸發完成:asyncContext.complete();

2、Spring Web MVC:

  • Web MVC視圖:模板引擎(jsp、themleaf)、內容協商、異常處理(BasicErrorController)等
  • Web MVC REST:資源服務(GetMapping),資源跨域(CrossOrigin)、服務發現(HATEOS)等
  • Web MVC 核心:核心框架、處理流程、核心組件(DispatcherServlet、HeadlerMapping、HandlerAdepter、ViewResolver)

3、Spring Web Flux:

Reactor基礎:Java Lambda、Mono、FLux

Web Flux核心:Web MVC註解、函數式聲明(RouterFunction)、異步非阻塞

使用場景:Web Flux的優勢和限制

4、關係型數據:

JDBC:數據源。JDBCTemplate、自動裝配,pom:spring-boot-starter-jdbc

JPA:實體映射關係、實體操作、自動裝配,pom:spring-boot-starter-data-jpa

事物:Spring事物抽象、JDBC事物處理、自動裝配,pom:spring-tx

 

四、Spring Framework手動裝配

 

五、SpringBoot自動裝配

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章