SpringBoot系列0-開始
1.spring-boot-starter-parent
spring推薦我們按照下面的方式使用spring-boot-starter-parent,這個父Pom的作用是提供常用jar包版本控制的。開啟原始檔可以驚奇得發現,spring-boot-starter-parent的parent是 spring-boot-dependencies,spring-boot-dependencies的作用我們馬上會說。
<parent> <groupId> org.springframework.boot </ groupId> <artifactId> spring-boot-starter-parent </ artifactId> <version> 2.0.0.BUILD- SNAPSHOT </ version> </ parent>
但是在實際過程中我們一般使用自己公司的parent,如何解決這個問題呢?如下,在專案主POM中<dependencyManagement>節點下面定義。
<dependencyManagement>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement >
需要注意spring-boot-dependencies只能在dependencyManagement中宣告,而不能在子模組中出現,因為spring-boot-dependencies中沒有宣告引入任何jar包,只是一個pom配置,開啟maven倉庫可以看到,它裡面定義了很多依賴宣告。
如果想在專案中使用spring-boot-dependencies中的jar包,但是不用預設版本,需要在dependencyManagement中指定版本,並且需要在spring-boot-dependencies之前。
2.starters
startrts(啟動器)是裡面包含了一系列依賴描述,方便你在專案中使用。比如
if you want to getstarted using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.
官方啟動器一般是spring-boot-starter-*。
第三方啟動器命名一般是*-spring-boot-starter。
下面是一些常用的Starters.
名稱 | 描述 |
---|---|
核心入門者,包括自動配置支援,日誌記錄和YAML |
|
使用Apache ActiveMQ啟動JMS訊息傳遞 |
|
使用Spring AMQP和Rabbit MQ的入門者 |
|
使用Spring AOP和AspectJ進行面向方面程式設計的入門者 |
|
使用Apache Artemis開始JMS訊息傳遞 |
|
使用Spring Batch的入門者 |
|
Starter使用Spring Framework的快取支援 |
|
Starter使用Spring Cloud Connectors,可簡化Cloud Foundry和Heroku等雲平臺中的服務連線 |
|
入門使用Cassandra分散式資料庫和Spring Data Cassandra |
|
使用Cassandra分散式資料庫和Spring Data Cassandra Reactive的初學者 |
|
使用Couchbase面向文件的資料庫和Spring Data Couchbase的初學者 |
|
初級用於使用Couchbase面向文件的資料庫和Spring Data Couchbase Reactive |
|
使用Elasticsearch搜尋和分析引擎和Spring Data Elasticsearch的入門者 |
|
使用Spring資料JPA與Hibernate的入門者 |
|
使用Spring Data LDAP的入門者 |
|
入門使用MongoDB面向文件的資料庫和Spring Data MongoDB |
|
入門使用MongoDB面向文件的資料庫和Spring Data MongoDB Reactive |
|
初學者使用Neo4j圖形資料庫和Spring Data Neo4j |
|
使用Spring Data Redis和Lettuce客戶端使用Redis鍵值資料儲存的入門者 |
|
初學者使用Redis鍵值資料儲存以及Spring Data Redis反應器和Lettuce客戶端 |
|
Starter使用Spring Data REST通過REST公開Spring Data儲存庫 |
|
啟動Spring Data Solr使用Apache Solr搜尋平臺 |
|
使用FreeMarker檢視構建MVC Web應用程式的入門者 |
|
使用Groovy模板檢視構建MVC Web應用程式的入門者 |
|
使用Spring MVC和Spring HATEOAS構建基於超媒體的RESTful Web應用程式的入門者 |
|
使用Spring Integration的入門者 |
|
將JDBC與Tomcat JDBC連線池配合使用的初學者 |
|
使用JAX-RS和Jersey構建RESTful Web應用程式的入門者。替代方案 |
|
使用jOOQ訪問SQL資料庫的入門者。替代 |
|
用於閱讀和編寫json的初學者 |
|
使用Atomikos啟動JTA交易 |
|
使用Bitronix啟動JTA交易 |
|
春季啟動Narayana JTA初學者 |
|
Starter使用Java Mail和Spring Framework的電子郵件傳送支援 |
|
使用Mustache檢視構建Web應用程式的入門者 |
|
春季啟動石英起動器 |
|
Starter使用Spring Security |
|
Starter用於測試包含JUnit,Hamcrest和Mockito等庫的Spring Boot應用程式 |
|
使用Thymeleaf檢視構建MVC Web應用程式的入門者 |
|
通過Hibernate Validator使用Java Bean驗證的入門者 |
|
使用Spring MVC構建Web的初學者,包括RESTful應用程式。使用Tomcat作為預設的嵌入容器 |
|
使用Spring Web Services的入門者 |
|
使用Spring Framework的Reactive Web支援構建WebFlux應用程式的入門者 |
|
使用Spring Framework的WebSocket支援構建WebSocket應用程式的入門者 |
3.Auto-configuration
如在添加了jar包,並且沒有手動配置相關的bean,spring boot會自動配置到spring應用程式中。您需要選擇加入@EnableAutoConfiguration或 @SpringBootApplication註釋到您的某個@Configuration類來自動配置。
4.spirngbean和依賴注入
如果是將application class放在工程的根目錄,只需要在main class上添加註解@ComponentScan,並且不需要任何引數。那麼在專案中所有的元件【@Component, @Service, @Repository, @Controller】都會自動註冊為spring bean.