我想要一个默认情况下包含所有字段的不可变属性类。将财产纳入图书馆。默认情况下,我可以创建一个具有简单类型的不可变属性类,但不能使用复杂类型。有没有办法将复杂类型的默认值设置为不可变的 ConfigurationProperties 类?
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConfigurationProperties(prefix = "foo")
@ConstructorBinding
@Getter
public final class AnyProperties {
private final String something
private final AnySubProperties sub;
public AnyProperties(
@DefaultValue("foo") String something,
AnySubProperties sub // Any annotation here ? Like @DefaultValue
) {
this.something = something;
this.sub = sub; // Always null !
}
@Getter
public static final class AnySubProperties {
private String finalValue;
public AnySubProperties(@DefaultValue("bar") String finalValue) {
this.finalValue = finalValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
例如sub, …