不可不知!SpringBoot 3.4 中 @DefaultValue 注解的妙用與實(shí)戰(zhàn)
在 Spring Boot 3.4 中,@DefaultValue 注解為我們提供了更加靈活的配置方式,尤其是在配置屬性綁定時(shí)。在實(shí)際開發(fā)中,配置文件的屬性綁定通常會通過兩種方式來實(shí)現(xiàn),分別是 @ConfigurationProperties 和 @Value。盡管兩者都能夠完成綁定任務(wù),但通常推薦使用 @ConfigurationProperties,因?yàn)樗峁┝烁叩慕Y(jié)構(gòu)化和類型安全性。
@ConfigurationProperties VS @Value
@ConfigurationProperties 為配置文件屬性綁定提供了一個(gè)更清晰且類型安全的方式。它將配置文件中的屬性映射到 Java 類的字段上,增強(qiáng)了代碼的可讀性和可維護(hù)性。而 @Value 雖然也能夠完成綁定任務(wù),但通常只適用于簡單的屬性,并且在處理復(fù)雜綁定時(shí),它缺乏類型安全的保障。使用 @Value 時(shí),還可能需要額外的自定義類型轉(zhuǎn)換器。
因此,在 Spring Boot 項(xiàng)目中,為了提升代碼的可維護(hù)性和避免潛在的錯(cuò)誤,通常推薦使用 @ConfigurationProperties 來綁定配置屬性。通過這種方式,代碼結(jié)構(gòu)更加清晰,同時(shí)也能避免運(yùn)行時(shí)錯(cuò)誤。
使用構(gòu)造函數(shù)進(jìn)行屬性綁定
通常情況下,@ConfigurationProperties 會通過 setter 方法來進(jìn)行屬性綁定。然而,Spring Boot 還支持使用構(gòu)造函數(shù)來進(jìn)行屬性綁定,提供了更為靈活的綁定方式。以下示例演示了如何使用構(gòu)造函數(shù)進(jìn)行綁定:
package com.icoderoad.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "pack.app")
public class PackApp {
private String title;
private String version;
private Integer sno;
public PackApp(String title, String version, Integer sno) {
this.title = title;
this.version = version;
this.sno = sno;
}
// getters, setters
}
在這個(gè)示例中,PackApp 類將會使用構(gòu)造函數(shù)進(jìn)行屬性綁定。
處理多個(gè)構(gòu)造函數(shù)
當(dāng)配置類中存在多個(gè)構(gòu)造函數(shù)時(shí),Spring Boot 默認(rèn)會使用 setter 方法進(jìn)行綁定。如果希望指定使用某一個(gè)構(gòu)造函數(shù)進(jìn)行綁定,可以使用 @ConstructorBinding 注解:
package com.icoderoad.config;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "pack.app")
public class PackApp {
private String title;
private String version;
private Integer sno;
@ConstructorBinding
public PackApp(String title, String version) {
this.title = title;
this.version = version;
}
}
這種情況下,Spring Boot 會使用帶有兩個(gè)參數(shù)的構(gòu)造函數(shù)進(jìn)行屬性綁定。
默認(rèn)構(gòu)造函數(shù)與私有構(gòu)造函數(shù)
如果配置類只有一個(gè)有參構(gòu)造函數(shù),Spring Boot 會默認(rèn)使用這個(gè)構(gòu)造函數(shù)進(jìn)行綁定。如果不希望使用該構(gòu)造函數(shù)進(jìn)行綁定,可以將其設(shè)置為 private 或者使用 @Autowired 注解:
package com.icoderoad.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "pack.app")
public class PackApp {
private String title;
private String version;
private Integer sno;
@Autowired
public PackApp(MyBean bean) {
// 初始化代碼
}
}
通過這種方式,Spring Boot 會使用 setter 方法來完成屬性綁定。
@DefaultValue 注解的使用
當(dāng)使用構(gòu)造函數(shù)綁定時(shí),如果某些屬性在配置文件中沒有找到對應(yīng)的值,@DefaultValue 注解可以為這些屬性設(shè)置默認(rèn)值。
假設(shè)配置文件如下:
pack:
app:
title: xxxx.xxxx
version: 1.0.0
若配置文件中沒有定義 pack.app.sno 屬性,默認(rèn)情況下 sno 會被賦值為 null。但如果我們希望在沒有配置該屬性時(shí)使用默認(rèn)值,可以使用 @DefaultValue 注解:
package com.icoderoad.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.beans.factory.annotation.Value;
@ConfigurationProperties(prefix = "pack.app")
public class PackApp {
private String title;
private String version;
@DefaultValue("888")
private Integer sno;
public PackApp(String title, String version, Integer sno) {
this.title = title;
this.version = version;
this.sno = sno;
}
// getters, setters
}
運(yùn)行時(shí),如果 pack.app.sno 沒有在配置文件中提供值,sno 會自動使用默認(rèn)值 888。
嵌套屬性的默認(rèn)值
如果你的配置類包含嵌套屬性,也可以使用 @DefaultValue 為這些嵌套屬性設(shè)置默認(rèn)值。例如:
package com.icoderoad.config;
@ConfigurationProperties(prefix = "pack.app")
public class PackApp {
private String title;
private String version;
private Integer sno;
private Security security;
public PackApp(String title, String version, Integer sno, @DefaultValue("Security [username=null]") Security security) {
this.title = title;
this.version = version;
this.sno = sno;
this.security = security;
}
// getters, setters
}
如果 security 屬性沒有在配置文件中進(jìn)行配置,Spring Boot 會將其設(shè)置為一個(gè)默認(rèn)值,即 Security [username=null]。
嵌套集合屬性的默認(rèn)值
同樣,對于集合類型的嵌套屬性,也可以使用 @DefaultValue 注解設(shè)置默認(rèn)值。例如:
package com.icoderoad.config;
@ConfigurationProperties(prefix = "pack.app")
public class PackApp {
private List<String> roles;
public PackApp(String title, String version, Integer sno, @DefaultValue({"ADMIN", "MGR"}) List<String> roles) {
this.roles = roles;
}
}
如果 roles 屬性沒有在配置文件中提供,roles 將會使用默認(rèn)的 {"ADMIN", "MGR"} 值。
Record 類型與默認(rèn)值
Spring Boot 還支持使用 Record 類型作為配置類,并為 Record 類型的字段設(shè)置默認(rèn)值。例如:
package com.icoderoad.config;
@ConfigurationProperties(prefix = "pack.app")
public record AppRecord(String title, String version, @DefaultValue("999") Integer sno) {}
這種方式允許我們將默認(rèn)值應(yīng)用到 Record 類型的字段,增強(qiáng)了代碼的簡潔性和類型安全性。
總結(jié)
@DefaultValue 注解是 Spring Boot 3.4 中非常實(shí)用的功能,它使得我們在屬性綁定時(shí)能夠?yàn)槿笔У膶傩蕴峁┠J(rèn)值,減少了因缺失配置而產(chǎn)生的異常和錯(cuò)誤。結(jié)合構(gòu)造函數(shù)綁定和嵌套屬性的默認(rèn)值功能,@DefaultValue 注解為我們提供了更加靈活和安全的配置方式,極大地提升了代碼的可維護(hù)性與穩(wěn)定性。