What is the purpose of custom configuration properties in Spring Boot?
Custom configuration properties allow you to define your own settings in application.properties or application.yml files and bind them to Java classes for easy and type-safe access.
Click to reveal answer
beginner
How do you bind custom properties to a Java class in Spring Boot?
You create a Java class with fields matching the property names and annotate it with @ConfigurationProperties(prefix = "your.prefix"). Then, register it as a bean with @Component or @EnableConfigurationProperties.
Click to reveal answer
intermediate
What annotation is used to enable support for @ConfigurationProperties classes in Spring Boot?
The @EnableConfigurationProperties annotation enables support for @ConfigurationProperties classes, allowing Spring Boot to bind external properties to your Java beans.
Click to reveal answer
intermediate
Why is it recommended to use custom configuration properties instead of @Value annotations for many related settings?
Using custom configuration properties groups related settings in one class, making the code cleaner, easier to maintain, and type-safe compared to scattering many @Value annotations.
Click to reveal answer
advanced
How can you validate custom configuration properties in Spring Boot?
You add validation annotations like @NotNull or @Min on fields in your @ConfigurationProperties class and annotate the class with @Validated to enforce validation when properties are loaded.
Click to reveal answer
Which annotation do you use to bind external properties to a Java class in Spring Boot?
A@Value
B@ConfigurationProperties
C@ComponentScan
D@Autowired
✗ Incorrect
The @ConfigurationProperties annotation binds external properties to a Java class based on a prefix.
Where do you typically define custom configuration properties in a Spring Boot project?
Aapplication.properties or application.yml
BJava source files
CStatic HTML files
DDatabase tables
✗ Incorrect
Custom configuration properties are defined in application.properties or application.yml files.
What is the benefit of using @EnableConfigurationProperties?
AIt disables property binding
BIt scans for @Component classes
CIt injects beans automatically
DIt enables binding of @ConfigurationProperties classes
✗ Incorrect
@EnableConfigurationProperties activates support for @ConfigurationProperties classes.
How do you ensure that your custom configuration properties are validated?
AAdd @Validated on the class and validation annotations on fields
BUse @Value annotations only
CWrite manual validation code in main method
DNo validation is possible
✗ Incorrect
Adding @Validated and validation annotations like @NotNull ensures properties are checked when loaded.
What prefix would you use in @ConfigurationProperties(prefix = "app.settings") to bind properties?
Aapplication.properties
Bspring.config
Capp.settings
Dsettings.app
✗ Incorrect
The prefix in @ConfigurationProperties matches the start of property keys in the config files.
Explain how to create and use custom configuration properties in Spring Boot.
Think about how Spring Boot connects external settings to Java classes.
You got /5 concepts.
Describe how validation works for custom configuration properties in Spring Boot.
Consider how Spring Boot ensures your settings are correct before running.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @ConfigurationProperties in Spring Boot?
easy
A. To create REST endpoints automatically
B. To enable security features in the application
C. To define database connection pools
D. To bind external configuration properties to a Java class
Solution
Step 1: Understand the role of @ConfigurationProperties
This annotation is used to map external configuration values (from files like application.properties or application.yml) to fields in a Java class.
Step 2: Compare with other options
Options A, C, and D relate to other Spring Boot features but not to property binding.
Final Answer:
To bind external configuration properties to a Java class -> Option D
Quick Check:
Property binding = B [OK]
Hint: Remember: @ConfigurationProperties binds config files to classes [OK]
Common Mistakes:
Confusing @ConfigurationProperties with @RestController
Thinking it manages database connections
Assuming it enables security automatically
2. Which of the following is the correct way to enable @ConfigurationProperties support in a Spring Boot application?
easy
A. Add @SpringBootApplication on the properties class
B. Add @EnableConfigurationProperties on a configuration class or main class
C. Add @RestController on the properties class
D. Add @ComponentScan on the properties class
Solution
Step 1: Identify how to activate configuration properties
Spring Boot requires @EnableConfigurationProperties on a configuration or main class to enable binding of @ConfigurationProperties classes.
Step 2: Review other annotations
@ComponentScan scans components but does not enable properties binding. @RestController is for web controllers. @SpringBootApplication is for main app class but does not alone enable properties binding.
Final Answer:
Add @EnableConfigurationProperties on a configuration class or main class -> Option B
Quick Check:
Enable config properties = D [OK]
Hint: Use @EnableConfigurationProperties to activate property binding [OK]
3. Given the following properties in application.yml and class, what will be the output of System.out.println(appProperties.getTimeout())?
application.yml:
app:
timeout: 5000
Java class:
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private int timeout;
public int getTimeout() { return timeout; }
public void setTimeout(int timeout) { this.timeout = timeout; }
}
medium
A. null
B. 0
C. 5000
D. Compilation error
Solution
Step 1: Understand property binding from YAML to class
The app.timeout property in YAML is set to 5000. The class with @ConfigurationProperties(prefix = "app") binds this value to the timeout field.
Step 2: Check getter method output
Calling getTimeout() returns the bound value 5000, so the printed output is 5000.
Final Answer:
5000 -> Option C
Quick Check:
Bound value printed = 5000 [OK]
Hint: Property prefix matches class prefix to bind values correctly [OK]
Common Mistakes:
Forgetting to add setter method causes binding failure
Using wrong prefix in annotation
Expecting null or default instead of bound value
4. Identify the error in this custom properties class that prevents property binding:
@ConfigurationProperties(prefix = "server")
public class ServerProperties {
private String port;
public String getPort() { return port; }
// Missing setter method
}
medium
A. Missing setter method for 'port' prevents binding
B. Prefix 'server' is invalid and causes error
C. Class must be annotated with @Component
D. Field 'port' should be public for binding
Solution
Step 1: Check property binding requirements
Spring Boot binds properties using setters. Without a setter for 'port', the value cannot be set.
Step 2: Validate other options
The prefix 'server' is valid. The class does not require @Component if registered via @EnableConfigurationProperties. Fields should be private with getters/setters, not public.
Final Answer:
Missing setter method for 'port' prevents binding -> Option A
Quick Check:
Setter missing = binding fails [OK]
Hint: Always add setters for fields to enable binding [OK]
Common Mistakes:
Assuming prefix must be custom or different
Thinking @Component is always required
Making fields public instead of using setters
5. You want to create a nested configuration property for a mail server with host and port inside application.properties like this:
Which of the following Java classes correctly models this nested property using @ConfigurationProperties?
hard
A. @ConfigurationProperties(prefix = "mail")
public class MailProperties {
private Server server;
public static class Server {
private String host;
private int port;
// getters and setters
}
// getters and setters for server
}
B. @ConfigurationProperties(prefix = "mail.server")
public class MailProperties {
private String host;
private int port;
// getters and setters
}
C. @ConfigurationProperties(prefix = "mail")
public class MailProperties {
private String host;
private int port;
// getters and setters
}
D. @ConfigurationProperties(prefix = "mail.server")
public class MailProperties {
private Server server;
public static class Server {
private String host;
private int port;
// getters and setters
}
// getters and setters for server
}
Solution
Step 1: Understand nested property binding
Properties like mail.server.host require a class with prefix mail and a nested class Server to hold host and port.
Step 2: Analyze options
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
private Server server;
public static class Server {
private String host;
private int port;
// getters and setters
}
// getters and setters for server
} correctly uses @ConfigurationProperties(prefix = "mail") with a nested Server class. @ConfigurationProperties(prefix = "mail.server")
public class MailProperties {
private String host;
private int port;
// getters and setters
} flattens properties incorrectly. @ConfigurationProperties(prefix = "mail")
public class MailProperties {
private String host;
private int port;
// getters and setters
} misses nesting. @ConfigurationProperties(prefix = "mail.server")
public class MailProperties {
private Server server;
public static class Server {
private String host;
private int port;
// getters and setters
}
// getters and setters for server
} uses prefix mail.server but still nests, which is inconsistent.
Final Answer:
@ConfigurationProperties(prefix="mail") with nested Server class -> Option A
Quick Check:
Nested class with prefix mail = A [OK]
Hint: Use nested classes for nested property groups [OK]
Common Mistakes:
Using wrong prefix for nested properties
Flattening nested properties into one class
Missing getters/setters in nested class
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.