Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Bind the properties mail.host, mail.port and mail.retry.max-attempts to an immutable Java object. Show the class and how it is registered.

We write a record, MailProperties, annotated with @ConfigurationProperties("mail"), with components host and port and a nested record Retry holding maxAttempts. In Spring Boot 3, a class with a single parameterised constructor, which every record has, is bound through that constructor automatically. We register it with @ConfigurationPropertiesScan or @EnableConfigurationProperties(MailProperties.class), give defaults with @DefaultValue, for example @DefaultValue("3") int maxAttempts, and add spring-boot-configuration-processor to generate IDE metadata.

Why this question is asked

Immutable configuration objects are the modern way to read settings in Spring Boot. Once the application has started, nobody can change a value by accident, the object is safe to share between threads, and every value is set in one place. Interviewers ask candidates to write one because it checks several details at once: constructor binding, nesting, defaults, registration and metadata. In this answer, we will recap the binding rules, write the record step by step, register it, add defaults and see what happens when a value is missing, and finish with metadata generation.

1A Quick Recap of Constructor Binding

Spring Boot can bind properties in two ways. With setter binding, it creates the object with a no-argument constructor and calls a setter for each property, so the object is mutable. With constructor binding, it collects the values first and calls a constructor with all of them, so the fields can be final and the object never changes.

Since Spring Boot 3.0, constructor binding is used automatically when the class has a single constructor with parameters. A Java record always has exactly one canonical constructor, so records are bound through it without any extra annotation. The older @ConstructorBinding annotation is now needed only when a class has several constructors and we must say which one to use.

2Writing the Record

The properties are mail.host, mail.port and mail.retry.max-attempts. The prefix is mail, the first two map to simple components, and retry is a nested group, so it becomes a nested record. The kebab-case name max-attempts maps to the Java name maxAttempts through relaxed binding.

Java
package com.example.mail;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConfigurationProperties("mail")
public record MailProperties(
String host,
@DefaultValue("25") int port,
@DefaultValue Retry retry) {
public record Retry(@DefaultValue("3") int maxAttempts) {}
}
// application.yml
// mail:
// host: smtp.example.com
// port: 587
// retry:
// max-attempts: 5

Notice the empty @DefaultValue on the retry component. Without it, if no mail.retry.* property is set at all, retry would be null and the inner default of 3 would never apply. With it, Spring Boot creates a Retry from its own defaults, so props.retry().maxAttempts() is 3 even when the YAML has no retry block. Values are converted to the component types, so port becomes an int and binding fails with a clear message if someone writes port: abc.

3Registering the Class

A @ConfigurationProperties record is not a component, so component scanning does not pick it up. We register it in one of two ways. @ConfigurationPropertiesScan on the application class scans its package and sub-packages for @ConfigurationProperties types; @EnableConfigurationProperties lists the classes explicitly, which suits a library or an auto-configuration.

Java
@SpringBootApplication
@ConfigurationPropertiesScan // finds MailProperties in com.example and below
public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}
// or, explicitly:
@Configuration
@EnableConfigurationProperties(MailProperties.class)
class MailConfig {}
@Service
class Mailer {
Mailer(MailProperties props) {
props.host(); // smtp.example.com
props.port(); // 587
props.retry().maxAttempts(); // 5
}
}
Step throughHow the record becomes a bean
  1. Scan finds @ConfigurationProperties("mail")or @EnableConfigurationProperties lists it
  2. Collect mail.* from the Environmentfiles, env vars, command line, all relaxed
  3. Convert each value to the parameter type"587" -> 587
  4. Apply @DefaultValue for missing valuesport 25, maxAttempts 3
  5. Call the record constructorone immutable MailProperties bean

5 steps in order. Play them, or step through them one at a time.

Either one is enough. We do not add @Component to the record; Spring Boot registers it through these mechanisms and names the bean after the prefix and the class, for example mail-com.example.mail.MailProperties, which we rarely need because we inject it by type.

4Defaults and Missing Values

@DefaultValue holds the default as a string, which Spring Boot converts just like a value from a file. That keeps defaults next to the field they belong to, visible to anyone reading the class. A component without a default and without a value is bound as null, or zero for a primitive, which is often a silent problem: here, a missing mail.host would give host() == null and the first email would fail.

Java
// No properties set at all:
props.host(); // null
props.port(); // 25
props.retry().maxAttempts(); // 3
// Make host required and fail at startup instead:
@Validated
@ConfigurationProperties("mail")
public record MailProperties(@NotBlank String host, @DefaultValue("25") int port,
@DefaultValue @Valid Retry retry) { ... }
// APPLICATION FAILED TO START
// Property: mail.host Value: "null" Reason: must not be blank

So defaults handle values that have a sensible fallback, and validation handles values that must be provided. Adding @Validated requires the spring-boot-starter-validation dependency on the classpath.

5Generating Metadata for the IDE

The last step makes the properties discoverable. The spring-boot-configuration-processor annotation processor reads the class at compile time and writes META-INF/spring-configuration-metadata.json, which IDEs use for completion, type checks and documentation in application.yml. Javadoc on the components becomes the property description.

XML
<!-- pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
// Gradle
// annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")

There is a catch with recent JDKs: since Java 23, javac no longer runs annotation processors found on the classpath unless the build asks for it, so with Maven we may also need to list the processor under annotationProcessorPaths in the maven-compiler-plugin configuration. Check your build if the metadata file does not appear in target/classes.

6Follow-up questions

Could I use a class with final fields instead of a record?

Yes. A class with private final fields, one constructor taking all of them and getters is bound the same way. Records are simply shorter and give equals, hashCode and toString for free.

Why not use @Value for three properties?

We could, but we would lose validation, metadata and the single typed object that shows everything the mail feature reads. Three related values under one prefix are exactly the case @ConfigurationProperties was designed for.

What if the class has two constructors?

Then Spring Boot cannot tell which one to use for binding, so we annotate the intended one with @ConstructorBinding. With a single constructor, the annotation is not needed in Spring Boot 3.

7Conclusion

In this answer, we bound mail.host, mail.port and mail.retry.max-attempts to an immutable MailProperties record with a nested Retry record. Spring Boot 3 uses constructor binding automatically because the record has a single constructor, @DefaultValue supplies defaults, including an empty @DefaultValue so the nested record always exists, and the class is registered with @ConfigurationPropertiesScan or @EnableConfigurationProperties. The configuration processor then generates metadata so IDEs can complete and document the properties.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write a record called MailProperties annotated with @ConfigurationProperties("mail"). It has three components: String host, int port, and a nested record Retry with one component, int maxAttempts.

Then, if they want more

Relaxed binding maps max-attempts in YAML to maxAttempts. Because a record has a single canonical constructor, Spring Boot 3 uses constructor binding automatically, so no @ConstructorBinding is needed and the object is immutable. For defaults I put @DefaultValue on the components, for example @DefaultValue("25") on port and @DefaultValue("3") on maxAttempts.

I also put an empty @DefaultValue on the retry component, so that if no retry properties are set the nested record is still created with its defaults instead of being null. For a required value like host, I would add @Validated on the record and @NotBlank on host, so startup fails clearly if it is missing.

That needs the validation starter. To register it, I put @ConfigurationPropertiesScan on the application class, or @EnableConfigurationProperties(MailProperties.class) on a configuration class, which is the usual choice in a library. Then any bean can inject MailProperties through its constructor.

Finally I add spring-boot-configuration-processor as an optional dependency, or as an annotation processor path on newer JDKs, so the build generates metadata and the IDE can complete and document the mail properties.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, Java 23, Spring Boot 3.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Bind the properties mail.host, mail.port and mail.retry.max-attempts to an immutable...": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/d36378b3a6/