SpringBoot配置綁定方法詳解
配置綁定
所謂“配置綁定”就是把配置文件中的值與 JavaBean 中對(duì)應(yīng)的屬性進(jìn)行綁定。通常,我們會(huì)把一些配置信息(例如,數(shù)據(jù)庫(kù)配置)放在配置文件中,然后通過(guò) Java 代碼去讀取該配置文件,并且把配置文件中指定的配置封裝到 JavaBean(實(shí)體類)中。
SpringBoot提供了以下兩種方式進(jìn)行配置綁定:
- 使用@Value注解
- 使用@ConfigurationProperties注解
@Value基礎(chǔ)類型配置
當(dāng)我們只需要讀取配置文件中的某一個(gè)配置時(shí),可以通過(guò) @Value 注解獲取
語(yǔ)法
@Value("${key}")
數(shù)據(jù)類型 成員變量;
@Value注解
${property : default_value}
- 注入的是外部配置文件對(duì)應(yīng)的property
- 也可以自定義默認(rèn)值
#{obj}
- 是SpEl表達(dá)式
- 可以表示常量的值

application.properties配置信息
#Girl配置信息===================================
girl.name=林初初
girl.age=25
#數(shù)組和集合=====================================
girl.hobbyList=單車,跑步,爬山
girl.habbyArray=唱歌,跳舞
girl.habbySet=燒烤,火鍋,火鍋
#Map集合=======================================
girl.hobbyMap={name:'林初初',age:25}
#日期==========================================
girl.birthday1 = 2022/10/09
girl.birthday2 = 2022-10-09
girl.birthday3 = 2022-10-09 21:12:21
config/Girl實(shí)體類
@Component
@Data
public class Girl {
//:后默認(rèn)值
@Value("${girl.name:初初}")
private String name;
@Value("${girl.age}")
private Integer age;
@Value("${girl.hobbyList}")
private List<String> hobbyList;
@Value("${girl.habbyArray}")
private String[] hobbyArray;
@Value("${girl.habbySet}")
private Set<String> hobbySet;
@Value("#{${girl.hobbyMap}}")
private Map<String,Object> hobbyMap;
@Value("#{150+20}")
private int height;
@Value("${girl.birthday1}")
@JsonFormat(pattern="yyyy/MM/dd")
private Date birthday1;
@Value("${girl.birthday2}")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthday2;
@Value("${girl.birthday3}")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime birthday3;
}啟動(dòng)類App
@SpringBootApplication
public class App{
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
Girl bean = context.getBean(Girl.class);
System.out.println(bean);
}
}運(yùn)行結(jié)果



亂碼解決

@ConfigurationProperties注解
通過(guò) Spring Boot 提供的 @ConfigurationProperties 注解,可以將配置文件中的配置數(shù)據(jù)綁定到 JavaBean 中。

application.properties配置信息
注意:
boy.hobbyMap={name:'moming',age:18}這種格式,此注解解析不了
要分開(kāi)寫如:
boy.hobbyMap.name=moming
boy.hobbyMap.age=18
#Boy配置信息===================================
boy.name=沫洺
boy.age=18
boy.hobbyArray=單車,跑步,爬山
boy.hobbyMap.name=moming
boy.hobbyMap.age=18
boy.birthday=2022-10-09 21:12:21
config/Girl實(shí)體類
@Configuration
@ConfigurationProperties(prefix = "boy")
@Data
public class Boy {
private String name;
private int age;
private String[] hobbyArray;
private Map<String,Object> hobbyMap;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime birthday;
}啟動(dòng)類App
@SpringBootApplication
public class App{
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
Boy bean = context.getBean(Boy.class);
System.out.println(bean);
}
}運(yùn)行結(jié)果

- 只有在容器中的組件,才會(huì)擁有 SpringBoot 提供的強(qiáng)大功能。如果我們想要使用 @ConfigurationProperties 注解進(jìn)行配置綁定,那么首先就要保證該JavaBean 對(duì)象在 IoC 容器中,所以需要用到 @Configuration 注解來(lái)添加組件到容器中。
- JavaBean 上使用了注解 @ConfigurationProperties(prefix = "boy") ,它表示將這個(gè) JavaBean 中的所有屬性與配置文件中以“boy”為前綴的配置進(jìn)行綁定。
@PropertySource
如果將所有的配置都集中到 application.properties 或 application.yml 中,那么這個(gè)配置文件會(huì)十分的臃腫且難以維護(hù),因此我們通常會(huì)將與 Spring Boot 無(wú)關(guān)的配置(例如自定義配置)提取出來(lái),寫在一個(gè)單獨(dú)的配置文件中,并在對(duì)應(yīng)的 JavaBean 上使用 @PropertySource 注解指向該配置文件。

自定義配置文件user.properties
user.username=初初
user.age=18
user.birthday=2000/12/01
user.books={jsp:36,html:360,spring:65}
user.hobby=23,34
user.list=sprig,myatis
user.set=34,45,56
實(shí)體配置類User
@Component
@Data
@PropertySource("classpath:user.properties")
public class User {
@Value("${user.username}")
private String username;
@Value("${user.age}")
private Integer age;
@Value("${user.birthday}")
@DateTimeFormat(pattern = "yyyy/MM/dd")
private LocalDate birthday;
@Value("#{${user.books}}")
private Map books;
@Value("${user.hobby}")
private Integer[] hobby;
@Value("${user.list}")
private List<String> list;
@Value("${user.set}")
private Set<Integer> set;
}啟動(dòng)測(cè)試App
@SpringBootApplication
public class App{
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
User bean = context.getBean(User.class);
System.out.println(bean);
}
}注意:該注解只能引用properties文件
到此這篇關(guān)于SpringBoot配置綁定方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot配置綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用FFmpeg提取音頻的實(shí)現(xiàn)示例
在Java開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到需要使用FFmpeg來(lái)處理音視頻文件的情況,本文主要介紹了java使用FFmpeg提取音頻的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
Gradle構(gòu)建基本的Web項(xiàng)目結(jié)構(gòu)
這篇文章主要為大家介紹了Gradle創(chuàng)建Web項(xiàng)目基本的框架結(jié)構(gòu)搭建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼
延時(shí)隊(duì)列場(chǎng)景在我們?nèi)粘I(yè)務(wù)開(kāi)發(fā)中經(jīng)常遇到,它是一種特殊類型的消息隊(duì)列,本文就來(lái)介紹一下SpringBoot實(shí)現(xiàn)redis延遲隊(duì)列的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析
這篇文章主要為大家介紹了Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
詳解SpringBoot?Start組件開(kāi)發(fā)之記錄接口日志信息
這篇文章主要為大家介紹了SpringBoot-Start組件開(kāi)發(fā)之記錄接口日志信息詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)的id值
當(dāng)往mysql數(shù)據(jù)庫(kù)插入一條數(shù)據(jù)時(shí),有時(shí)候需要知道剛插入的信息,下面這篇文章主要給大家介紹了關(guān)于mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)id值的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法
本文主要介紹了 Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

