天天看點

江帥帥:精通 Spring Boot 系列 03

  1. 關閉指定的自動配置

通過上面的 @EnableAutoConfiguration 注解就能根據指定的依賴,自動進行配置。但如果你想關閉某一項自動配置,就需要使用 @SpringBootApplication 下的 exclude 參數來設定。比如,我想關閉 DataSource,代碼具體如下:

1@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

  1. 自定義啟動 banner

能滿足企業定制 logo 或項目啟動圖案。比如,可以借助如下幾個網站來生成:

1

http://patorjk.com/software/taag

2

http://www.network-science.de/ascii/

3

http://www.kammerl.de/ascii/AsciiSignature.php

生成之後,直接将複制好的圖案,放到建立的 banner.txt 檔案中。運作程式之後,具體顯示效果如下:

  1. 全局配置檔案

一般使用 application.properties 或者 application.yml 檔案來當作全局配置檔案。它能被添加在下面幾個目錄下,差別是加載的順序是不同的,具體如下:

1項目根目錄的 /config 目錄下

2項目根目錄下

3類路徑的 /config 目錄下

4類路徑下

比如,可以在 application.properties 配置檔案中,設定端口、請求字尾等内容。具體的配置參數可以參考官網文檔第 10 章 Appendices:

https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/#common-application-properties
  1. 配置 Web 容器

在 Spring Boot 應用中,可以内置 Tomcat、Netty、Jetty 等容器。

1)配置 Tomcat

如果添加了 spring-boot-starter-web 依賴,則項目會預設使用 Tomcat 作為 Web 容器。

針對 Tomcat 可以添加一些配置,具體配置如下:

1# 端口

2server.port=80

3# 錯誤跳轉路徑

4server.error.path

5# session 失效時間

6server.servlet.session.timeout

7# 項目名稱

8server.servlet.context-path

9# 編碼,一般 utf-8

10server.tomcat.uri-encoding=utf-8

11# ...

2)配置 Jetty

在 Spring Boot 應用中嵌入 Jetty 的配置很簡單,把 spring-boot-starter-web 中的 Tomcat 改成 Jetty 即可,具體配置如下:

3 org.springframework.boot

4 spring-boot-starter-web

5

6

7 org.springframework.boot

8 spring-boot-starter-tomcat

9

10

11

12

13

14

15 org.springframework.boot

16 spring-boot-starter-jetty

17

3)配置 Undertow

Undertow 是紅帽公司開發的一款基于 NIO 的高性能 Web 嵌入式伺服器,擁有非常好的性能。配置方式具體如下:

16 spring-boot-starter-undertow

  1. HTTPS 配置

使用 JDK 提供的 keytool 工具,可以生成一個數字證書,具體指令如下:

1keytool -genkey -alias httpskey -keyalg RSA -keysize 2048 -keystore hello.p12 -validity 365

-genkey 表示建立一個密鑰

-alias httpskey 設定密鑰的别名

-keyalg RSA 表示使用的加密算法是 RSA

-keysize 2048 設定密鑰的長度

-keystore hello.p12 設定密鑰存放位置

-validity 365 設定密鑰的有效時間

然後在 application.properties 檔案中編輯,具體配置如下:

1server.ssl.key-store=hello.p12

2server.ssl.key-alias=httpskey

3server.ssl.key-store-password=123456

  1. 類型安全配置

前面也說過,我們的配置檔案可以使用 properties 配置和 yaml 配置,項目啟動後,它們都會被加載到 Spring 的環境中,如果你要用配置資訊的話,直接使用 @Value 注解即可。

但資料注入到屬性中,需要注意安全。Spring Boot 使用類型安全配置屬性,即使在資料量非常龐大的情況下,将配置檔案中的資料注入到 Bean 裡也是很友善的。

1user.name=翠花

2user.age=18

3user.address=北京

添加對應的 Bean 類,使用 @ConfigurationProperties 注解來使用配置,通過 prefix 屬性來描述要加載的配置檔案的字首,具體如下:

1@Component

2@ConfigurationProperties(prefix="user")

3public class User {

4 private String name;

5 private Integer age;

6 private String address;

7 // get 和 set 方法...

8}

  1. YAML 配置

YAML 是專門用來寫配置檔案的語言,非常簡潔、強大,類似 JSON。它可用來替換 application.properties 檔案。YAML 主要是由 spring-boot-starter-web 依賴子產品中的 snakeyaml 依賴進行解析。但它不能使用 @propertySource 注解加載 YAML 檔案,否則還要使用 Properties 配置。

舉個小案例,具體寫法如下:

1server:

2 port:80

3 servlet:

4 context-path:/hello

5 tomcat:

6 uri-encoding:utf-8

還可以自定義配置,具體寫法如下:

1user:

2 name:翠花

3 age:18

對應的代碼,跟前面的 User 類一樣,具體源碼如下:

6 // get 和 set 方法...

7}

還能設定成集合的樣式,集合中是單個值,具體寫法如下:

4 aihao:

5 - 燙頭

6 - 捏腳

7 - Reading

對應的代碼,具體源碼如下:

6 private List aihao;

也能先設定成集合的樣式,但集合中是對象,具體寫法如下:

1shop:

2 users:

3 - name:翠花

4 age:18

5 aihao:

6 - 燙頭

7 - 捏腳

8 - Reading

9 - name:小強

10 age:18

11 aihao:

12 - 燙頭

13 - 捏腳

14 - Reading

2@ConfigurationProperties(prefix="shop")

3public class Users {

4 private List users;

5 // get 和 set 方法...

6}

1public class User {

2 private String name;

3 private Integer age;

4 private List aihao;

  1. Profile 配置

如果我們在項目中,需要頻繁在開發環境、測試環境和生産環境中更改大量的配置,會讓你懷疑人生,是以我們使用 @Profile 注解來更加簡潔進行處理。

具體的命名規則:application-{xxx}.properties,這樣就能在不同環境下進行差別配置資訊。具體使用步驟如下:

第一步,添加配置檔案

在 resources 目錄中,建立 application-dev.properties 和 application-prod.properties 檔案,代表開發和生産環境中的配置。

1# 開發環境

2server.port=8080

1# 生産環境

2server.port=80

第二步,指定對應模式

1)在 application.properties 中指定

1# 開發時用 dev,生産時用 prod

2spring.profiles.active=dev

2)在啟動類 main 方法中指定

1SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootApp.class);

3builder.application().setAdditionalProfiles("prod");

4

5builder.run(args);

3)也可以在項目啟動時配置

在我們将項目打成 jar 檔案再啟動,具體的操作指令如下:

1java -jar springdemo-xxx.jar --spring.profiles.active=prod

來源于:奈學開發者社群-江帥帥