SpringBoot實(shí)現(xiàn)給屬性賦值的兩種方式
一,介紹
在Spring Boot中,配置文件是用來(lái)設(shè)置應(yīng)用程序的各種參數(shù)和操作模式的重要部分。Spring Boot支持兩種主要類(lèi)型的配置文件:properties文件和YAML 文件。這兩種文件都可以用來(lái)定義相同的配置,但它們?cè)诟袷胶捅磉_(dá)能力上有所不同。
二,Properties 配置方式
properties文件是Java平臺(tái)最傳統(tǒng)的配置方式,文件擴(kuò)展名為 .
properties。這種格式非常簡(jiǎn)單,主要由鍵值對(duì)組成,每一對(duì)鍵值對(duì)設(shè)置一個(gè)配置屬性。
示例:
定義模型Person類(lèi):
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix="person") public class Person { private String name; private int age; private String uuid; private Dog dog; // standard getters and setters public static class Dog { private String name; private String breed; // standard getters and setters } }
Properties 配置
person.name=John Doe person.age=35 person.uuid=${random.uuid} person.dog.name=Rex person.dog.breed=Labrador
這樣配置后,Spring Boot 會(huì)自動(dòng)application.properties
中的相關(guān)配置注入到 Person對(duì)象和其內(nèi)部的 Dog對(duì)象。
使用 @Value注解也可以直接在 Spring Boot 應(yīng)用中注入配置值,例
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Person { @Value("${person.name}") private String name; @Value("${person.age}") private int age; @Value("${person.uuid}") private String uuid; // 內(nèi)部類(lèi)和其他配置略 }
三,YAML 配置方式
YAML 是一種層次結(jié)構(gòu)化的數(shù)據(jù)格式,相比于 properties文件,它支持列表和嵌套的對(duì)象,使得配置更加清晰和組織化。
yaml配置:
person: name: "John Doe" age: 35 uuid: ${random.uuid} dog: name: "Rex" breed: "Labrador"
這時(shí)要將YAML文件中的配置自動(dòng)映射到一個(gè)Java類(lèi)中,需要在Spring Boot應(yīng)用中定義相應(yīng)的配置類(lèi),并使用@ConfigurationProperties注解。
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Configuration @ConfigurationProperties(prefix = "person") public class Person { private String name; private int age; private String uuid; private Dog dog; @Component public static class Dog { private String name; private String breed; // getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } } }
四,對(duì)比
1. 可讀性
- YAML 由于其支持層級(jí)結(jié)構(gòu),通常在表達(dá)更復(fù)雜的配置時(shí)更加清晰和易讀。
- Properties 文件更適合簡(jiǎn)單的平面鍵值對(duì),但在需要表達(dá)嵌套配置時(shí)可讀性較差。
2. 表達(dá)能力
- YAML 支持復(fù)雜的數(shù)據(jù)結(jié)構(gòu),如列表和字典(即嵌套的對(duì)象),這使得它在表達(dá)如安全規(guī)則、路由配置等復(fù)雜配置時(shí)非常有用。
- Properties 文件不支持直接的層級(jí)或復(fù)雜結(jié)構(gòu),所有結(jié)構(gòu)都必須通過(guò)點(diǎn)分隔的方式平鋪開(kāi)來(lái)表達(dá)。
3. 錯(cuò)誤檢測(cè)
- YAML 文件由于格式更加復(fù)雜,對(duì)縮進(jìn)非常敏感,錯(cuò)誤的縮進(jìn)可能導(dǎo)致整個(gè)文件無(wú)法解析。
- Properties 文件結(jié)構(gòu)簡(jiǎn)單,縮進(jìn)和格式錯(cuò)誤的容忍度較高。
4. 使用場(chǎng)景
- 如果配置較為簡(jiǎn)單,或是遷移遺留項(xiàng)目而不希望引入新的復(fù)雜性,那么使用
.
properties可能更合適。 - 對(duì)于新項(xiàng)目或需要表達(dá)復(fù)雜配置的情況,
.
yaml提供了更強(qiáng)的表達(dá)能力和更好的可讀性。
以上就是SpringBoot實(shí)現(xiàn)給屬性賦值的兩種方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot給屬性賦值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java 出現(xiàn)問(wèn)題javax.servlet.http.HttpServlet was not found解決方法
這篇文章主要介紹了java 出現(xiàn)問(wèn)題javax.servlet.http.HttpServlet was not found解決方法的相關(guān)資料,需要的朋友可以參考下2016-11-11springboot整合kaptcha生成驗(yàn)證碼功能
這篇文章主要介紹了springboot整合kaptcha生成驗(yàn)證碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Java實(shí)現(xiàn)設(shè)計(jì)模式之責(zé)任鏈模式
責(zé)任鏈模式是一種行為設(shè)計(jì)模式,允許你將請(qǐng)求沿著處理鏈發(fā)送,然后處理者都可對(duì)其進(jìn)行處理,完成后可以再將其傳遞給下一個(gè)處理者。下面將會(huì)舉例說(shuō)明什么是責(zé)任鏈模式,責(zé)任鏈模式該如何使用2022-08-08