天天看點

spring boot 2.0之使用spring boot

每一個spring boot的發型版本都包含了所依賴的版本,如果更新spring boot版本,其依賴也會同步更新更新。maven的使用者可以通過繼承spring-boot-starter-parent。其包含了一些合理的值的設定:

1. 預設設定的編譯器為JDK 1.8

2. 源檔案預設UTF-8編碼

4. 合理的資源過濾(filter)

5. 合理的插件配置

6. 對于application.properties的合理的過濾

7. 預設的配置檔案接收spring的${}表達式,maven的資源過濾符被修改為@..@,可以使用maven屬性resource.delimiter進行設定更改。

實作starter-parent的方式有如下兩種:

1. 繼承starter-parent的配置檔案如下:

也可以在自己的pom檔案中修改指定的依賴版本資訊:例如要更新 Spring Data 為另一個版本。具體代碼如下

2. 在<code>dependencyManagement</code> 中加入依賴管理:如下代碼所示:

(備注:type表名導入的類型為POM,scope=import:完全導入POM的依賴)

starter是應用中一系列依賴的描述,可以不用太多依賴就能擷取相應的依賴包。官方給出的starter的命名方式為:spring-boot-starter-*,非官方定義的starter不應當以spring-boot-starter開始,而應當以項目工程為開始例如:thirdpartyproject-spring-boot-starter

starters的清單如下:

名稱

描述

spring-boot-starter

核心starter,包含了自動配置支援,logging以及yaml

 spring-boot-starter-activemq

 使用apache ACTIVEMQ的JMS處理STARTER

 spring-boot-starter-amqp

 使用spring AMQP以及rabbit MQ的starter

 spring-boot-starter-aop

 使用spring AOP以及APJECT J的starter

 spring-boot-starter-artemis

 使用apache Artemis作為JMS的starter

 spring-boot-starter-batch

 使用spring batch的starter

 spring-boot-starter-cache

 支援spring framwork cache的starter

 spring-boot-starter-cloud-connectors

 spring cloud connectors的連接配接雲平台couchbase服務的連接配接器,像like Cloud Foundry 與Heroku

 spring-boot-starter-data-cassandra

 使用cassandra分布式資料庫以及Spring Data Cassandra的starter

 spring-boot-starter-data-cassandra-reactive

 使用cassandra分布式資料庫以及Spring Data Cassandra 反應的starter

 spring-boot-starter-data-couchbase

 使用面向對象的資料庫以及Spring Data Couchbase的資料庫

 spring-boot-starter-data-couchbase-reactive

 與前面類似

 spring-boot-starter-data-elasticsearch

 使用彈性搜尋以及分析引擎spring DAta Elasticsearch

 spring-boot-starter-data-jpa

 使用hibernate的 spring JPA starter

 spring-boot-starter-data-ldap

  Spring Data LDAP 的starter

 spring-boot-starter-data-mongodb

 使用mongodb的starter

 spring-boot-starter-data-mongodb-reactive

 spring-boot-starter-data-neo4j

 使用Neo4j graph database 與 Spring Data Neo4j的starter

 spring-boot-starter-data-redis

 使用key,value存儲在Data Redis的資料庫

 spring-boot-starter-data-redis-reactive

 spring-boot-starter-data-rest

 通過使用spring data rest暴露在spring data repostory。

 spring-boot-starter-data-solr

 通過使用apache solr平台的starter

 spring-boot-starter-freemarker

 通過使用freemarker的MVC web 應用的starter

 spring-boot-starter-groovy-templates

 使用groovy template建立的MVC web應用

 spring-boot-starter-hateoas

 使用Spring MVC 與 Spring HATEOAS建構的超媒體的starter

 spring-boot-starter-integration

 spring 內建starter

 spring-boot-starter-jdbc

 使用tomcat的jdbc的連接配接池連結JDBC的starter

 spring-boot-starter-jersey

 使用JAX-RS 與 Jersey建構的web應用程式,也可以使用spring-boot-starter-web

 spring-boot-starter-jooq

 使用jooq連結資料庫的starter,其可以替換為:spring-boot-starter-jdbc或者是spring-boot-starter-data-jpa

 spring-boot-starter-json

 讀或者寫json的starter

 spring-boot-starter-jta-atomikos

 使用atomikos的JTA事務starter

 spring-boot-starter-jta-bitronix

 使用bitronix的事務starter

 spring-boot-starter-jta-narayana

 使用narayana的JTA事務starter

 spring-boot-starter-mail

 使用jmail與spring framwork 支援的方式發送郵件

 spring-boot-starter-mustache

 shiyong mustache建立對應的web應用

 spring-boot-starter-quartz

 定時調用的starter

 spring-boot-starter-security

 安全的starter

 spring-boot-starter-test

 測試的starter

 spring-boot-starter-thymeleaf

 使用thymeleaf建構MVC web應用的starter

 spring-boot-starter-validatior

 通過使用hibernate驗證器驗證的starter

 spring-boot-starter-web

 使用tomcat作為預設容器建構包括的restful,使用MVC的web應用的starter

 spring-boot-starter-web-services

 使用web services的starter

 spring-boot-starter-webflux

 使用 Spring Framework’s Reactive Web support支援的方式建構WebFlux 應用

 spring-boot-starter-websocket

 通過使用Spring Framework’s WebSocket support建構dewebsocker的starter

通過以下starter可以監控和管理應用:

spring-boot-starter-actuator

提供生産的一些特性來進行監控和管理的starter

spring boot不需要指定的具體的代碼結構,但是這兒有一些常用的一些架構:

1. 使用預設包:使用預設包的話,會導緻<code>@ComponentScan</code>, <code>@EntityScan</code>, 或者 <code>@SpringBootApplication</code>注冊掃描産生一定的問題。是以常推薦的包結構為:

2. 主要main應用程式的位置:推薦放到root package的目錄上。@EnableAutoConfiguration注解将放在main類上,用于搜尋包裡面的某些指定項(例如:JPA的包,将會搜尋@entity标簽)。使用@ComponentScan注解可以不用指定包名。如果為根包的話,還可以使用@SpringBootApplication标簽。其包結構示例如下:

類Application.java 使用@Configuration聲明為啟動類:其代碼如下:

1. 盡管基于XML的spring配置是可用,這裡還是推薦基于java的配置。

2. 主要的資源都要用@Configuration進行配置。

3. 使用@Import可以導入一個主要的資源類。

4. 對于依賴的jar的配置:可以通過<code>@EnableAutoConfiguration</code> or <code>@SpringBootApplication</code>進行配置加載對應的配置資料。

5. 自動配置沒有侵入性,在任何時候,你都可以自定義你的配置去替換掉自動配置部分。

6. 如果要找到哪些自動配置被應用,可以使用debug的方式運作程式,可以通過日志在控制台顯示出對應的報告資訊。

7. 如果不想使用自動配置,可以使用exclude進行排除(也可以通過spring.autoconfigure.exclude處理)代碼如下:

8. 當使用了@ComponentScan,所有的( (<code>@Component</code>, <code>@Service</code>, <code>@Repository</code>, <code>@Controller</code> etc.)都會被注入spring bean中。通過使用@<code>Autowired</code> 可以自動裝配對象。

9.spring boot 提供了 <code>@SpringBootApplication</code>來對需要通知引入(<code>@Configuration</code>, <code>@EnableAutoConfiguration</code>, 與 <code>@ComponentScan</code>)進行了簡化:代碼如下:

01. 在IDE中運作(eclipse,intel idea)

02. 打包運作

 也可以進行遠端調試:代碼如下

03. 通過maven運作:

可以通過設定環境變量來進行設定處理(export MAVEN_OPTS=-Xmx1024m)

spring-boot-devtools

1. spring boot 提供了一系列的工具來友善其進行開發,spring-boot-devtools提供了開發的便利,可以通過以下引入依賴子產品:代碼如下

2. 在生産環境中,常常使用緩存來提升性能,但在開發環境下,該配置是不可用的,是以spring-boot-devtools預設會設定其不可用。可以通過在application.properties中配置對應的參數:Thymeleaf 提供了spring.thymeleaf.cache設定,spring-boot-devtools能夠自動監控。

3. 應用程式可以通過spring-boot-devtools來監控classpath上的變化來自動重新開機,這在IDE中相當有用的。靜态資源的改變不會被重新開機(eclipse:儲存修改的檔案會自動生效。intel idea:需要運作Build -&gt; Make Project)。

4. 每次重新開機的時候:都會列印一些日志報告資訊:可以通過spring.devtools.restart.log-condition-evaluation-delta=false不讓其列印。

5. 可以通過設定具體的哪些資源變化不會重新開機,其配置如下:spring.devtools.restart.exclude=static/**,public/**

6. 可以通過添加spring.devtools.restart.additional-paths路徑來動态增加。

7. 可以通過在application.propterties中或者在啟動類中加入spring.devtools.restart.enabled 屬性來控制器不重新開機。

8. 可以通過使用trigger檔案在指定隻有該檔案修改的時候,才會觸發重新開機:其配置為spring.devtools.restart.trigger-file

9. 可以在META-INF/spring-devtools.propertie中加入如下配置自動化其配置:代碼如下:

10. 可以在根路徑下加入spring-boot-devtools.properties檔案,自定義其裡面的配置。

11. 如果遠端調試的話,需要在maven的plugin中指定如下

 設定其對應的spring.devtools.remote.secret屬性。

文檔參考位址:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference