1. 程式人生 > >SpringBoot系列0-開始

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.

名稱 描述

spring-boot-starter

核心入門者,包括自動配置支援,日誌記錄和YAML

spring-boot-starter-activemq

使用Apache ActiveMQ啟動JMS訊息傳遞

spring-boot-starter-amqp

使用Spring AMQP和Rabbit MQ的入門者

spring-boot-starter-aop

使用Spring AOP和AspectJ進行面向方面程式設計的入門者

spring-boot-starter-artemis

使用Apache Artemis開始JMS訊息傳遞

spring-boot-starter-batch

使用Spring Batch的入門者

spring-boot-starter-cache

Starter使用Spring Framework的快取支援

spring-boot-starter-cloud-connectors

Starter使用Spring Cloud Connectors,可簡化Cloud Foundry和Heroku等雲平臺中的服務連線

spring-boot-starter-data-cassandra

入門使用Cassandra分散式資料庫和Spring Data Cassandra

spring-boot-starter-data-cassandra-reactive

使用Cassandra分散式資料庫和Spring Data Cassandra Reactive的初學者

spring-boot-starter-data-couchbase

使用Couchbase面向文件的資料庫和Spring Data Couchbase的初學者

spring-boot-starter-data-couchbase-reactive

初級用於使用Couchbase面向文件的資料庫和Spring Data Couchbase Reactive

spring-boot-starter-data-elasticsearch

使用Elasticsearch搜尋和分析引擎和Spring Data Elasticsearch的入門者

spring-boot-starter-data-jpa

使用Spring資料JPA與Hibernate的入門者

spring-boot-starter-data-ldap

使用Spring Data LDAP的入門者

spring-boot-starter-data-mongodb

入門使用MongoDB面向文件的資料庫和Spring Data MongoDB

spring-boot-starter-data-mongodb-reactive

入門使用MongoDB面向文件的資料庫和Spring Data MongoDB Reactive

spring-boot-starter-data-neo4j

初學者使用Neo4j圖形資料庫和Spring Data Neo4j

spring-boot-starter-data-redis

使用Spring Data Redis和Lettuce客戶端使用Redis鍵值資料儲存的入門者

spring-boot-starter-data-redis-reactive

初學者使用Redis鍵值資料儲存以及Spring Data Redis反應器和Lettuce客戶端

spring-boot-starter-data-rest

Starter使用Spring Data REST通過REST公開Spring Data儲存庫

spring-boot-starter-data-solr

啟動Spring Data Solr使用Apache Solr搜尋平臺

spring-boot-starter-freemarker

使用FreeMarker檢視構建MVC Web應用程式的入門者

spring-boot-starter-groovy-templates

使用Groovy模板檢視構建MVC Web應用程式的入門者

spring-boot-starter-hateoas

使用Spring MVC和Spring HATEOAS構建基於超媒體的RESTful Web應用程式的入門者

spring-boot-starter-integration

使用Spring Integration的入門者

spring-boot-starter-jdbc

將JDBC與Tomcat JDBC連線池配合使用的初學者

spring-boot-starter-jersey

使用JAX-RS和Jersey構建RESTful Web應用程式的入門者。替代方案spring-boot-starter-web

spring-boot-starter-jooq

使用jOOQ訪問SQL資料庫的入門者。替代spring-boot-starter-data-jpaspring-boot-starter-jdbc

spring-boot-starter-json

用於閱讀和編寫json的初學者

spring-boot-starter-jta-atomikos

使用Atomikos啟動JTA交易

spring-boot-starter-jta-bitronix

使用Bitronix啟動JTA交易

spring-boot-starter-jta-narayana

春季啟動Narayana JTA初學者

spring-boot-starter-mail

Starter使用Java Mail和Spring Framework的電子郵件傳送支援

spring-boot-starter-mustache

使用Mustache檢視構建Web應用程式的入門者

spring-boot-starter-quartz

春季啟動石英起動器

spring-boot-starter-security

Starter使用Spring Security

spring-boot-starter-test

Starter用於測試包含JUnit,Hamcrest和Mockito等庫的Spring Boot應用程式

spring-boot-starter-thymeleaf

使用Thymeleaf檢視構建MVC Web應用程式的入門者

spring-boot-starter-validation

通過Hibernate Validator使用Java Bean驗證的入門者

spring-boot-starter-web

使用Spring MVC構建Web的初學者,包括RESTful應用程式。使用Tomcat作為預設的嵌入容器

spring-boot-starter-web-services

使用Spring Web Services的入門者

spring-boot-starter-webflux

使用Spring Framework的Reactive Web支援構建WebFlux應用程式的入門者

spring-boot-starter-websocket

使用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.


5.用@SpringBootApplication簡化註解

  @SpringBootApplication等價於@Configuration,@EnableAutoConfiguration和@ComponentScan三者之和。