Bird
0
0

You want to create a nested configuration property for a mail server with host and port inside application.properties like this:

hard📝 Application Q15 of 15
Spring Boot - Application Configuration
You want to create a nested configuration property for a mail server with host and port inside application.properties like this:
mail.server.host=smtp.example.com
mail.server.port=587
Which of the following Java classes correctly models this nested property using @ConfigurationProperties?
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 }
Step-by-Step Solution
Solution:
  1. 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.
  2. 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.
  3. Final Answer:

    @ConfigurationProperties(prefix="mail") with nested Server class -> Option A
  4. Quick Check:

    Nested class with prefix mail = A [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes