Spring Boot易學難精
易學
- 組件自動裝配:規約大于配置,專注核心業務
- 外部化配置:一次構建、按需調配,到處運行
- 嵌入式容器:內紙容器、無序部署、獨立運行
- Spring Boot Stater:簡化依賴、按需裝配、自我包含
- Production-Ready:一站式運維、生態無縫整合
難精
- 組件自動裝配:模式注解、@Enable模塊、條件裝配、加載機制
- 外部化配置:Environment抽象、生命周期、破壞性變更
- 嵌入式容器:Servlet Web容器、Reactive Web容器
- Spring Boot Stater:依賴管理、裝配條件、裝配順序
- Production-Ready:健康檢查、數據指標、@Endpoint管控
Spring Boot三大特性
- 組件自動裝配:Web MVC、 Web Flux和JDBC
- 嵌入式Web容器:Tomcat、Jetty和所以及(Undertow)
- 生產準備特性:指標、健康檢查、外部化配置等
核心特性
組件自動裝配
激活:@EnableAutoConfiguration
配置:/META--INF/spring.factories
實現:xxxAutoConfiguartion
?在Spring Boot項目中都有如下的啟動類:
最重要的就是@SpringBootApplication和SpringApplication.run(),其中
@SpringBootApplication注解部分:
?當spring boot掃描到@EnableAutoConfiguration注解時
則會將spring-boot-autoconfigure.jar/META-INF/spring.factories文件中org.springframework.boot.autoconfigure.EnableAutoConfiguration對應的value里的所有xxxConfiguration類加載到IOC容器中。
而xxxAutoConfiguration類一般都會有@ConditionalOnxxx注解,通過這些條件注解來判斷是否真正的創建xxxConfiguration對象。
spring boot的各種spring-boot-starter-xxx.jar也正是居于此注解來達到自動裝配的目的。
@EnableAutoConfiguration注解部分:
其中最關鍵的要屬@Import(AutoConfigurationImportSelector.class),借助AutoConfigurationImportSelector,@EnableAutoConfiguration可以幫助SpringBoot應用將所有符合條件的@Configuration配置都加載到當前SpringBoot創建并使用的IoC容器。
嵌入式Web容器
web Servlet:Tomcat、Jetty和Undertow
web Reactive:Netty Web Server
生產準備特性
指標:/actuator/metrics
健康檢查:/actuator/health
外部化配置:/actuator/configprops