Spring Boot詳解配置文件的用途與用法
1. SpringBoot 配置文件
1.1 配置文件的作用
配置文件中配置了項(xiàng)目中重要的數(shù)據(jù), 例如:
- 數(shù)據(jù)庫(kù)的連接信息 (用戶名密碼)
- 項(xiàng)目的啟動(dòng)端口
- 第三方系統(tǒng)的調(diào)用密鑰等信息
- 用于發(fā)現(xiàn)和定位問(wèn)題的普通日志和異常日志等
Spring Boot項(xiàng)目沒(méi)有配置信息, 就不能連接和操作數(shù)據(jù)庫(kù), 甚至不能保存可以用于排查問(wèn)題的關(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 配置文件說(shuō)明
1.3.1 properties 基本語(yǔ)法
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 注解來(lái)實(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 配置文件說(shuō)明
yml 是 YMAL 是縮寫(xiě), 它的全稱是 Yet Another Markup Language, 譯為 另一種標(biāo)記語(yǔ)言.
1.4.1 yml 基本語(yǔ)法
yml 是樹(shù)形結(jié)構(gòu)的配置文件, 它的基礎(chǔ)語(yǔ)法是 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的另一種寫(xiě)法(行內(nèi)寫(xiě)法)
配置對(duì)象
student: {id: 1,name: zhangsan,age: 18}
配置集合
mylist: {colors: [RED,GREEN,BLACK]}
1.5 properties 和 yml 比較
properties 的語(yǔ)法更復(fù)雜, yml 語(yǔ)法更簡(jiǎn)潔

yml通用性更好, 支持更多的語(yǔ)言, 如 Java, Go, Python等
yml支持更多的數(shù)據(jù)類型
yml格式的配置文件寫(xiě)的時(shí)候容易出錯(cuò)(在:之后有一個(gè)空格), 而properties寫(xiě)法傳統(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
直接在類上寫(xiě)
@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中的ArrayList(擴(kuò)容機(jī)制)詳解
ArrayList作為Java中廣泛使用的動(dòng)態(tài)數(shù)組,其擴(kuò)容機(jī)制是保證性能和內(nèi)存使用平衡的關(guān)鍵,默認(rèn)初始容量為10,擴(kuò)容因子為1.5,旨在減少頻繁的內(nèi)存分配和數(shù)據(jù)遷移代價(jià),同時(shí)建議使用預(yù)估計(jì)的初始化容量以減少擴(kuò)容次數(shù)2024-11-11
idea中java及java web項(xiàng)目的常見(jiàn)問(wèn)題及解決
在IDEA中處理亂碼問(wèn)題主要涉及四個(gè)方面:文件編碼設(shè)置為UTF-8、編輯器默認(rèn)編碼調(diào)整、Tomcat運(yùn)行配置編碼設(shè)置以及解決cmd中的亂碼,此外,詳細(xì)介紹了在IDEA中創(chuàng)建Web項(xiàng)目的步驟,包括新建Java工程、添加Web框架支持、添加Tomcat依賴庫(kù)2024-09-09
springboot注解Aspect實(shí)現(xiàn)方案
本文提供一種自定義注解,來(lái)實(shí)現(xiàn)業(yè)務(wù)審批操作的DEMO,不包含審批流程的配置功能。對(duì)springboot注解Aspect實(shí)現(xiàn)方案感興趣的朋友一起看看吧2022-01-01
java雙端隊(duì)列之ArrayDequeue原理講解
這篇文章主要為大家介紹了java雙端隊(duì)列之ArrayDequeue原理講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Spring MVC 文件、cookies的接收 與REST響應(yīng)詳
在SpringMVC中,使用@RequestPart注解可接收文件并處理多部分請(qǐng)求,同時(shí)可以通過(guò)@CookieValue和HttpServletResponse來(lái)獲取和設(shè)置Cookies,本文介紹Spring MVC 文件、cookies的接收 與REST響應(yīng),感興趣的朋友跟隨小編一起看看吧2024-09-09
Eclipse/MyEclipse轉(zhuǎn)IntelliJ IDEA完全攻略(圖文)
這篇文章主要介紹了Eclipse/MyEclipse轉(zhuǎn)IntelliJ IDEA完全攻略(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

