Spring Boot詳解配置文件的用途與用法
1. SpringBoot 配置文件
1.1 配置文件的作用
配置文件中配置了項(xiàng)目中重要的數(shù)據(jù), 例如:
- 數(shù)據(jù)庫的連接信息 (用戶名密碼)
- 項(xiàng)目的啟動(dòng)端口
- 第三方系統(tǒng)的調(diào)用密鑰等信息
- 用于發(fā)現(xiàn)和定位問題的普通日志和異常日志等
Spring Boot項(xiàng)目沒有配置信息, 就不能連接和操作數(shù)據(jù)庫, 甚至不能保存可以用于排查問題的關(guān)鍵日志. 所以配置文件非常重要.
1.2 配置文件的格式
在Spring Boot 中配置文件主要分為兩種:
.properties
(主要是key=value格式).yml
(主要是key: value格式)
注意:
- 當(dāng)項(xiàng)目中既有
.properties
和.yml
, 且兩個(gè)配置文件中有相同的配置項(xiàng), Spring Boot 會(huì)優(yōu)先考慮.properties
, 因?yàn)?.properties
的優(yōu)先級(jí)更高一些. - 一個(gè)項(xiàng)目中允許存在兩種不同的配置文件,
.properties
.yml
, 但是在項(xiàng)目中建議只使用一種配置文件的格式.
1.3 properties 配置文件說明
1.3.1 properties 基本語法
properties 是以 key=value
這種格式配置的.
server.port=9090
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/2022-6-1
spring.datasource.username=root
spring.datasource.password=1234
配置文件的注釋信息使用 “#”
1.3.2 讀取配置文件
讀取配置文件的內(nèi)容, 可以使用 @Value
注解來實(shí)現(xiàn)
@Value
注解使用 "${}"
的格式讀取.
@Component public class Read implements InitializingBean { @Value("${server.port}") private String port; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println(port); System.out.println(); } }
1.4 yml 配置文件說明
yml 是 YMAL 是縮寫, 它的全稱是 Yet Another Markup Language, 譯為 另一種標(biāo)記語言.
1.4.1 yml 基本語法
yml 是樹形結(jié)構(gòu)的配置文件, 它的基礎(chǔ)語法是 key: value
, 這里的:
后面跟著一個(gè)空格.
server:
port: 9090
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/2022-6-1
username: root
password: 1234
1.4.2 yml 使用進(jìn)階
# ~代表null
null.value: ~
查看一段代碼
string:
str1: Hello \n World
str2: 'Hello \n World'
str3: "Hello \n World"
讀取yml中的這段代碼
@Component public class Read1 implements InitializingBean { @Value("${string.str1}") private String str1; @Value("${string.str2}") private String str2; @Value("${string.str3}") private String str3; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println("str1: "+str1); System.out.println("str2: "+str2); System.out.println("str3: "+str3); System.out.println(); } }
運(yùn)行結(jié)果:
字符串加上雙引號(hào), 會(huì)執(zhí)行\(zhòng)n 換行.
1.4.3 配置對(duì)象
yml 中配置對(duì)象
student:
id: 1
name: zhangsan
age: 18
讀取配置的對(duì)象, 就需要用到另一個(gè)注解: @ConfigurationProperties
@Component @ConfigurationProperties("student") public class User { private int id; private String name; private int age; // 一堆getter setter }
讀取
@Component public class Read2 implements InitializingBean { @Autowired private Student student; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println(student); System.out.println(); } }
1.4.4 配置集合
yml 中 配置集合
mylist:
colors:
- RED
- GREEN
- BLACK
讀取配置集合
@Component @ConfigurationProperties("mylist") public class MyList { private List<String> colors; // 一堆getter 和 setter }
打印代碼
@Component public class Read3 implements InitializingBean { @Autowired private MyList myList; @Override public void afterPropertiesSet() throws Exception { for (String val : myList.getColors()){ System.out.println(val); } } }
1.4.5 yml的另一種寫法(行內(nèi)寫法)
配置對(duì)象
student: {id: 1,name: zhangsan,age: 18}
配置集合
mylist: {colors: [RED,GREEN,BLACK]}
1.5 properties 和 yml 比較
properties
的語法更復(fù)雜, yml
語法更簡(jiǎn)潔
yml通用性更好, 支持更多的語言, 如 Java, Go, Python等
yml支持更多的數(shù)據(jù)類型
yml格式的配置文件寫的時(shí)候容易出錯(cuò)(在:之后有一個(gè)空格), 而properties寫法傳統(tǒng)比較復(fù)制,但不太容易出錯(cuò)
2. 讀取 SpringBoot 配置文件的方法
2.1 使用 @Value 讀取配置文件
只能讀取一個(gè)
@Component public class Read implements InitializingBean { @Value("${server.port}") private String port; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println(port); System.out.println(); } }
2.2 使用@ConfigurationProperties
直接在類上寫
@Component @ConfigurationProperties("mylist") public class MyList { private List<String> colors; public List<String> getColors() { return colors; } public void setColors(List<String> colors) { this.colors = colors; } }
2.3 @PropertySource讀取指定配置文件
jdbc.username=root2
jdbc.password=root1
@Component @PropertySource(value = {"classpath:application.properties"}) public class JDBC implements InitializingBean { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Override public void afterPropertiesSet() throws Exception { System.out.println(username + " " + password); } }
到此這篇關(guān)于Spring Boot詳解配置文件的用途與用法的文章就介紹到這了,更多相關(guān)Spring Boot配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中如何實(shí)現(xiàn)對(duì)類的對(duì)象進(jìn)行排序
在本篇文章里小編給各位整理一篇關(guān)于java中如何實(shí)現(xiàn)對(duì)類的對(duì)象進(jìn)行排序知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-02-02SpringBoot配置@Configuration注解和@bean注解
這篇文章主要介紹了SpringBoot配置@Configuration注解和@bean注解,文章圍繞主題相關(guān)內(nèi)容展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04java判斷l(xiāng)ist不為空的實(shí)現(xiàn),和限制條數(shù)不要在一起寫
這篇文章主要介紹了java判斷l(xiāng)ist不為空的實(shí)現(xiàn),和限制條數(shù)不要在一起寫。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06Mybatis如何自動(dòng)生成數(shù)據(jù)庫表結(jié)構(gòu)總結(jié)
這篇文章主要給大家介紹了關(guān)于Mybatis如何自動(dòng)生成數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Springboot如何設(shè)置靜態(tài)資源緩存一年
這篇文章主要介紹了Springboot如何設(shè)置靜態(tài)資源緩存一年,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11