Master Spring Boot 3 Config: @ConfigurationProperties, Constructor Binding & Default Values
This article introduces a comprehensive Spring Boot 3 case collection and provides a detailed guide on using @ConfigurationProperties, constructor binding, @DefaultValue, and record types for type‑safe configuration, including examples of nested properties, collections, and default values, while promising ongoing updates and free source code for subscribers.
Overview
This article announces a Spring Boot 3 case collection (PDF) and provides a step‑by‑step guide on configuration property binding in Spring Boot.
1. @ConfigurationProperties vs @Value
Spring Boot supports two ways to bind configuration properties: @ConfigurationProperties and @Value . Although both work, @ConfigurationProperties is generally recommended because it offers a structured, type‑safe approach.
2. Constructor Binding
2.1 Multiple constructors
When a configuration class defines several constructors, Spring Boot falls back to setter‑based binding. To force a specific constructor, annotate it with @ConstructorBinding . Only one constructor may carry this annotation.
<code>@ConstructorBinding
public PackApp(String title, String version) {
this.title = title;
this.version = version;
}
</code>2.2 Single constructor
If there is only one parameterized constructor, it is used automatically. To prevent constructor binding, you can annotate the constructor with @Autowired or make it private .
<code>@Autowired
public PackApp(MyBean bean) {
// TODO
}
</code>2.3 Record types
Constructor binding also works with Java records introduced in JDK 16.
<code>@ConfigurationProperties(prefix = "pack.app")
public record AppRecord(String title, String version, Integer sno) {}
</code>3. @DefaultValue annotation
3.1 Basic usage
When a property is missing, @DefaultValue supplies a fallback value.
<code>public PackApp(String title, String version,
@DefaultValue("888") Integer sno) { … }
</code>3.2 Nested property default
For nested objects, @DefaultValue can create an empty instance instead of null .
<code>public PackApp(String title, String version,
Integer sno,
@DefaultValue Security security) { … }
</code>3.3 Collection defaults
Collections can receive default values as well.
<code>public PackApp(String title, String version,
Integer sno,
@DefaultValue({"ADMIN","MGR"}) List<String> roles) { … }
</code>3.4 Default values in records
Records can also define defaults.
<code>public record AppRecord(String title,
String version,
@DefaultValue("999") Integer sno) {}
</code>The article concludes by reminding readers that the PDF collection will be continuously updated and that subscribers receive the source code and full documentation for free.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.