💂 個人主頁: Java程式魚
💬 如果文章對你有幫助,歡迎關注、點贊、收藏(一鍵三連)和訂閱專欄
👤 微信號:hzy1014211086,如果你正在學習Spring Boot,可以加入我們的Spring技術交流群,共同成長
| 序號 | 內容 |
|---|---|
| 1 | 面試題專欄 |
| 2 | Redis專欄 |
| 3 | SpringBoot專欄 |
| 3 | SpringBoot專欄更新計劃 |
文章目錄
- 一、簡介
- 二、properties
- 自定義屬性
- 引數間參考
- 自定義組態檔地址
- 外部配置
- 優先級
- 多環境配置
- 三、YAML
- 四、原始碼
一、簡介
Spring Boot 的核心是自動配置(或者叫默認配置),通過自動配置大大減少Spring專案的配置撰寫,但是在實際開發中,我們仍然需要根據需求來適當修改某些必要的引數配置,這時Spring Boot提供了兩種格式的配置方便開發者進行修改,
- applicaiton*.properties
- application*.yml(或者application*.yaml)
二、properties
自定義屬性
我們除了可以在 Spring Boot 的組態檔中設定各個 Starter 模塊中預定義的配置屬性,也可以在組態檔中定義一些我們需要的自定義屬性,比如在 application.properties 中添加:
blog.id=1
blog.title=SpringBoot
blog.author=fish
然后,在應用中我們可以通過@Value注解來加載這些自定義的引數,比如:
@Data
@Component
public class Blog {
@Value("${blog.id}")
private Long id;
@Value("${blog.title}")
private String title;
@Value("${blog.author}")
private String author;
}
大家有沒有發現一個問題,如果一個物件屬性太多,一個一個系結到屬性欄位上是不是太麻煩,如何解決呢?
@Data
@Component
@ConfigurationProperties(prefix = "blog")
public class Blog2 {
private Long id;
private String title;
private String author;
}
這里配置完還需要在 Spring Boot 啟動類加上@EnableConfigurationProperties 并指明要加載哪個bean
@EnableConfigurationProperties({Blog2.class})
@SpringBootApplication
public class Chapter2Application {
public static void main(String[] args) {
SpringApplication.run(Chapter2Application.class, args);
}
}
引數間參考
在application.properties中的各個引數之間也可以直接參考來使用,就像下面的設定:
blog.id=1
blog.title=SpringBoot
blog.author=fish
blog.desc=${blog.author} wrote an article about ${blog.title}
@RestController
public class TestController {
@Autowired
private Blog2 blog2;
@RequestMapping("/test2")
public String test2() {
return blog2.toString();
}
}
@Data
@Component
@ConfigurationProperties(prefix = "blog")
public class Blog2 {
private Long id;
private String title;
private String author;
private String desc;
}

自定義組態檔地址
有時候我們不希望把所有配置都放在 application.properties 里面,這時候我們可以另外定義一個,這里我明取名為 blog.properties ,路徑跟也放在 src/main/resources 下面,
custom.blog.id=2
custom.blog.title=SpringBoot custom config
custom.blog.author=fish
custom.blog.desc=${blog.author} wrote an article about ${blog.title}
@RestController
public class TestController {
@Autowired
private CustomBlog customBlog;
@RequestMapping("/test3")
public String test3() {
return customBlog.toString();
}
}
@Data
@Component
@ConfigurationProperties(prefix = "custom.blog")
@PropertySource("classpath:blog.properties")
public class CustomBlog {
private Long id;
private String title;
private String author;
private String desc;
}

外部配置
SpringBoot 可以使用命令 java -jar 命令來啟動 SpringBoot 應用,該命令除了啟動應用之外,還可以在命令列中來指定應用的引數,比如:java -jar 專案名.jar --server.port=8081,直接以命令列的方式,來設定 server.port 屬性,另啟動應用的埠設為 8081,
可以看出,命令列中連續的兩個減號–就是對application.properties中的屬性值進行賦值的標識,所以java -jar 專案名.jar --server.port=8081 等價于在 application.properties 中添加屬性server.port=8081,
如果你怕命令列有風險,可以使用 SpringApplication.setAddCommandLineProperties(false) 禁用它,
實際上,Spring Boot 應用程式有多種設定途徑,Spring Boot 能從多重屬性源獲得屬性,包括如下幾種:
- 在您的主目錄(當 devtools 被激活,則為 ~/.spring-boot-devtools.properties)中的 Devtools 全域設定屬性,
- 在測驗中使用到的 @TestPropertySource 注解,
- 在測驗中使用到的 properties 屬性,可以是 @SpringBootTest 和用于測驗應用程式某部分的測驗注解,
- 命令列引數,
- 來自 SPRING_APPLICATION_JSON 的屬性(嵌入在環境變數或者系統屬性【system propert】中的行內 JSON),
- ServletConfig 初始化引數,
- ServletContext 初始化引數,
- 來自 java:comp/env 的 JNDI 屬性,
- Java 系統屬性(System.getProperties()),
- 作業系統環境變數,
- 只有 random.* 屬性的 RandomValuePropertySource,
- 在已打包的 jar 外部的指定 profile 的應用屬性檔案(application-{profile}.properties 和 YAML 變數),
- 在已打包的 jar 內部的指定 profile 的應用屬性檔案(application-{profile}.properties 和 YAML 變數),
- 在已打包的 jar 外部的應用屬性檔案(application.properties 和 YAML 變數),
- 在已打包的 jar 內部的應用屬性檔案(application.properties 和 YAML 變數),
- 在 @Configuration 類上的 @PropertySource 注解,
- 默認屬性(使用 SpringApplication.setDefaultProperties 指定),
這里串列按組優先級排序,也就是說,任何在高優先級屬性源里設定的屬性都會覆寫低優先級的相同屬性,列如我們上面提到的命令列屬性就覆寫了application.properties 的屬性,
在我平時作業中,用的比較多的方式是YAML、application.properties、jar命令列,其他的都很少用
優先級
我們創建一個 Spring Boot 工程時,默認 resources 目錄下就有一個 application.properties 檔案,可以在 application.properties 檔案中進行專案配置,但是這個檔案并非唯一的組態檔,在 Spring Boot 中,一共有 4 個地方可以存放 application.properties 檔案,
- 當前專案根目錄下的 config 目錄下
- 當前專案的根目錄下
- resources 目錄下的 config 目錄下
- resources 目錄下
加載優先級:當前專案根目錄下的 config 目錄下 > 當前專案的根目錄下 > resources 目錄下的 config 目錄下 > resources 目錄下
也就是說,src/main/resources/config 下 application.properties 覆寫 src/main/resources 下application.properties 中相同的屬性
多環境配置
當應用程式需要部署到不同運行環境時,一些配置細節通常會有所不同,例如日志級別、資料源、各種密碼等等,如果按照以前的做法,就是每次發布的時候替換掉組態檔,這樣太麻煩了,如何解決呢?
在 Spring Boot 中多環境組態檔名需要滿足 application-{profile}.properties 的格式,其中{profile}對應你的環境標識,比如:
- application-dev.properties:開發環境
- application-test.properties:測驗環境
- application-pre.properties:灰度環境
- application-prod.properties:生產環境
想要使用對應的環境,只需要在application.properties中使用spring.profiles.active屬性來設定,值對應上面提到的{profile},例如:spring.profiles.active=dev 就會加載 application-dev.properties 組態檔內容,
至于哪個具體的組態檔會被加載,需要在application.properties檔案中通過spring.profiles.active屬性來設定,其值對應組態檔中的{profile}值,如:spring.profiles.active=test就會加載application-test.properties組態檔內容,
當然您也可以用命令列啟動的時候帶上引數:
java -jar 專案名稱.jar --spring.profiles.active=dev
application.properties
spring.profiles.active=dev
application-dev.properties
blog.id=3
blog.title=SpringBoot profiles dev config
blog.author=fish
blog.desc=${blog.author} wrote an article about ${blog.title}
application-pro.properties
blog.id=4
blog.title=SpringBoot profiles pro config
blog.author=fish
blog.desc=${blog.author} wrote an article about ${blog.title}
@RestController
public class TestController {
@Autowired
private Blog blog;
@RequestMapping("/test")
public String test() {
return blog.toString();
}
}
@Data
@Component
public class Blog {
@Value("${blog.id}")
private Long id;
@Value("${blog.title}")
private String title;
@Value("${blog.author}")
private String author;
@Value("${blog.desc}")
private String desc;
}

三、YAML
YAML采用的配置格式不像properties的配置那樣以單純的鍵值對形式來表示,而是以類似大綱的縮進形式來表示,比如:下面的一段YAML配置資訊
blog:
id: 1
title: SpringBoot
author: fish
其他知識點和 properties 幾乎一樣,只是配置方式稍有不同,在這里YAML的使用我就不重復闡述了,
注意點:
(1)無法使用 @PropertySource 注解加載 YAML 檔案,因此,如果您需要以這種方式加載值,請使用屬性檔案(properties),
(2)SpringBoot 2.4版本之前,我們在 YAML 組態檔中,使用spring.profiles來定義不同環境的標識,而在2.4版本升級之后,我們需要將spring.profiles配置用spring.config.activate.on-profile替代,
SpringBoot 2.4 版本對組態檔加載機制做了很多變化,有興趣的小伙伴可以去 Spring 官網查看,
四、原始碼
本文的相關例子可以查看下面倉庫中的 chapter2 目錄:
- Gitee:https://gitee.com/hezhiyuan007/spring-boot-study
- Github:https://github.com/java-fish-0907/spring-boot-study
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/303915.html
標籤:其他
上一篇:小白也能做的選擇(上)
