Spring Boot詳解配置文件的用途與用法
1. SpringBoot 配置文件
1.1 配置文件的作用
配置文件中配置了項目中重要的數(shù)據(jù), 例如:
- 數(shù)據(jù)庫的連接信息 (用戶名密碼)
- 項目的啟動端口
- 第三方系統(tǒng)的調(diào)用密鑰等信息
- 用于發(fā)現(xiàn)和定位問題的普通日志和異常日志等
Spring Boot項目沒有配置信息, 就不能連接和操作數(shù)據(jù)庫, 甚至不能保存可以用于排查問題的關(guān)鍵日志. 所以配置文件非常重要.
1.2 配置文件的格式
在Spring Boot 中配置文件主要分為兩種:
.properties(主要是key=value格式).yml(主要是key: value格式)
注意:
- 當項目中既有
.properties和.yml, 且兩個配置文件中有相同的配置項, Spring Boot 會優(yōu)先考慮.properties, 因為.properties的優(yōu)先級更高一些. - 一個項目中允許存在兩種不同的配置文件,
.properties.yml, 但是在項目中建議只使用一種配置文件的格式.
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 注解來實現(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, 譯為 另一種標記語言.
1.4.1 yml 基本語法
yml 是樹形結(jié)構(gòu)的配置文件, 它的基礎(chǔ)語法是 key: value, 這里的:后面跟著一個空格.
server:
port: 9090
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/2022-6-1
username: root
password: 1234
1.4.2 yml 使用進階
# ~代表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();
}
}運行結(jié)果:
字符串加上雙引號, 會執(zhí)行\(zhòng)n 換行.

1.4.3 配置對象
yml 中配置對象
student:
id: 1
name: zhangsan
age: 18
讀取配置的對象, 就需要用到另一個注解: @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)寫法)
配置對象
student: {id: 1,name: zhangsan,age: 18}
配置集合
mylist: {colors: [RED,GREEN,BLACK]}
1.5 properties 和 yml 比較
properties 的語法更復(fù)雜, yml 語法更簡潔

yml通用性更好, 支持更多的語言, 如 Java, Go, Python等
yml支持更多的數(shù)據(jù)類型
yml格式的配置文件寫的時候容易出錯(在:之后有一個空格), 而properties寫法傳統(tǒng)比較復(fù)制,但不太容易出錯
2. 讀取 SpringBoot 配置文件的方法
2.1 使用 @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();
}
}
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot配置@Configuration注解和@bean注解
這篇文章主要介紹了SpringBoot配置@Configuration注解和@bean注解,文章圍繞主題相關(guān)內(nèi)容展開詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-04-04
java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫
這篇文章主要介紹了java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06
Mybatis如何自動生成數(shù)據(jù)庫表結(jié)構(gòu)總結(jié)
這篇文章主要給大家介紹了關(guān)于Mybatis如何自動生成數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Mybatis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-11-11
Springboot如何設(shè)置靜態(tài)資源緩存一年
這篇文章主要介紹了Springboot如何設(shè)置靜態(tài)資源緩存一年,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11

