SpringBoot 啟動器 介紹及其作用
Spring Boot應用啟動器基本的一共有44種,具體如下:
1)spring-boot-starter
這是Spring Boot的核心啟動器,包含了自動配置、日誌和YAML。
2)spring-boot-starter-actuator
幫助監控和管理應用。
3)spring-boot-starter-amqp
通過spring-rabbit來支援AMQP協議(Advanced Message Queuing Protocol)。
4)spring-boot-starter-aop
支援面向方面的程式設計即AOP,包括spring-aop和AspectJ。
5)spring-boot-starter-artemis
通過Apache Artemis支援JMS的API(Java Message Service API)。
6)spring-boot-starter-batch
支援Spring Batch,包括HSQLDB資料庫。
7)spring-boot-starter-cache
支援Spring的Cache抽象。
8)spring-boot-starter-cloud-connectors
支援Spring Cloud Connectors,簡化了在像Cloud Foundry或Heroku這樣的雲平臺上連線服務。
9)spring-boot-starter-data-elasticsearch
支援ElasticSearch搜尋和分析引擎,包括spring-data-elasticsearch。
10)spring-boot-starter-data-gemfire
支援GemFire分散式資料儲存,包括spring-data-gemfire。
11)spring-boot-starter-data-jpa
支援JPA(Java Persistence API),包括spring-data-jpa、spring-orm、Hibernate。
12)spring-boot-starter-data-mongodb
支援MongoDB資料,包括spring-data-mongodb。
13)spring-boot-starter-data-rest
通過spring-data-rest-webmvc,支援通過REST暴露Spring Data資料倉庫。
14)spring-boot-starter-data-solr
支援Apache Solr搜尋平臺,包括spring-data-solr。
15)spring-boot-starter-freemarker
支援FreeMarker模板引擎。
16)spring-boot-starter-groovy-templates
支援Groovy模板引擎。
17)spring-boot-starter-hateoas
通過spring-hateoas支援基於HATEOAS的RESTful Web服務。
18)spring-boot-starter-hornetq
通過HornetQ支援JMS。
19)spring-boot-starter-integration
支援通用的spring-integration模組。
20)spring-boot-starter-jdbc
支援JDBC資料庫。
21)spring-boot-starter-jersey
支援Jersey RESTful Web服務框架。
22)spring-boot-starter-jta-atomikos
通過Atomikos支援JTA分散式事務處理。
23)spring-boot-starter-jta-bitronix
通過Bitronix支援JTA分散式事務處理。
24)spring-boot-starter-mail
支援javax.mail模組。
25)spring-boot-starter-mobile
支援spring-mobile。
26)spring-boot-starter-mustache
支援Mustache模板引擎。
27)spring-boot-starter-redis
支援Redis鍵值儲存資料庫,包括spring-redis。
28)spring-boot-starter-security
支援spring-security。
29)spring-boot-starter-social-facebook
支援spring-social-facebook
30)spring-boot-starter-social-linkedin
支援pring-social-linkedin
31)spring-boot-starter-social-twitter
支援pring-social-twitter
32)spring-boot-starter-test
支援常規的測試依賴,包括JUnit、Hamcrest、Mockito以及spring-test模組。
33)spring-boot-starter-thymeleaf
支援Thymeleaf模板引擎,包括與Spring的整合。
34)spring-boot-starter-velocity
支援Velocity模板引擎。
35)spring-boot-starter-web
S支援全棧式Web開發,包括Tomcat和spring-webmvc。
36)spring-boot-starter-websocket
支援WebSocket開發。
37)spring-boot-starter-ws
支援Spring Web Services。
Spring Boot應用啟動器面向生產環境的還有2種,具體如下:
1)spring-boot-starter-actuator
增加了面向產品上線相關的功能,比如測量和監控。
2)spring-boot-starter-remote-shell
增加了遠端ssh shell的支援。
最後,Spring Boot應用啟動器還有一些替換技術的啟動器,具體如下:
1)spring-boot-starter-jetty
引入了Jetty HTTP引擎(用於替換Tomcat)。
2)spring-boot-starter-log4j
支援Log4J日誌框架。
3)spring-boot-starter-logging
引入了Spring Boot預設的日誌框架Logback。
4)spring-boot-starter-tomcat
引入了Spring Boot預設的HTTP引擎Tomcat。
5)spring-boot-starter-undertow
引入了Undertow HTTP引擎(用於替換Tomcat)。
原理
首先說說原理,我們知道使用一個公用的starter的時候,只需要將相應的依賴新增的Maven的配置檔案當中即可,免去了自己需要引用很多依賴類,並且SpringBoot會自動進行類的自動配置。那麼 SpringBoot 是如何知道要例項化哪些類,並進行自動配置的呢? 下面簡單說一下。
第一步,SpringBoot 在啟動時會去依賴的starter包中尋找resources/META-INF/spring.factories
檔案,然後根據檔案中配置的Jar包去掃描專案所依賴的Jar包,這類似於 Java 的SPI機制。
第二步,根據spring.factories
配置載入AutoConfigure
類。
最後,根據@Conditional
註解的條件,進行自動配置並將Bean注入Spring Context上下文當中。
我們也可以使用@ImportAutoConfiguration({MyServiceAutoConfiguration.class})
指定自動配置哪些類。