Bird
Raised Fist0
Spring Bootframework~20 mins

Custom configuration properties in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing a custom property in Spring Boot?
Given a Spring Boot application with a custom configuration property app.title=MyApp defined in application.properties and a @ConfigurationProperties class binding it, what will be the output of System.out.println(appProperties.getTitle())?
Spring Boot
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String title;
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
}

// application.properties
// app.title=MyApp

// In some service or main method:
// System.out.println(appProperties.getTitle());
Aapp.title
Bnull
CMyApp
DCompilation error due to missing @EnableConfigurationProperties
Attempts:
2 left
💡 Hint
Remember that Spring Boot automatically binds properties with matching prefix to the fields in the class annotated with @ConfigurationProperties.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly enables custom configuration properties in Spring Boot?
You want to enable a class DatabaseSettings annotated with @ConfigurationProperties(prefix = "db") to be loaded as a bean. Which of the following code snippets correctly enables this?
A
@SpringBootApplication
@EnableAutoConfiguration
public class App {}
B
@SpringBootApplication
@ComponentScan(basePackages = "db")
public class App {}
C
@SpringBootApplication
@ConfigurationPropertiesScan
public class App {}
D
@SpringBootApplication
@EnableConfigurationProperties(DatabaseSettings.class)
public class App {}
Attempts:
2 left
💡 Hint
Use the annotation that explicitly enables configuration properties classes.
🔧 Debug
advanced
2:30remaining
Why does the custom configuration property not bind correctly?
Given the class below, why does the property server.port=8081 not bind to the port field?
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "server")
public class ServerConfig {
    private int port;
    public int getPort() { return port; }
    // Missing setter method
}
AMissing setter method for 'port' prevents Spring from setting the value.
BThe prefix 'server' is incorrect; it should be 'server.port'.
CThe class must be annotated with @Configuration instead of @Component.
DThe field 'port' must be public for binding to work.
Attempts:
2 left
💡 Hint
Spring Boot uses setters to inject property values.
❓ state_output
advanced
2:00remaining
What is the value of the list property after binding?
Given the following properties and class, what will be the value of appProperties.getServers()?
application.yml:
app:
  servers:
    - host1.example.com
    - host2.example.com

Java class:
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private List servers;
    public List getServers() { return servers; }
    public void setServers(List servers) { this.servers = servers; }
}
ACompilation error due to missing @EnableConfigurationProperties
B["host1.example.com", "host2.example.com"]
C["app.servers"]
Dnull
Attempts:
2 left
💡 Hint
YAML lists map to Java List fields with matching names.
🧠 Conceptual
expert
3:00remaining
Which statement about @ConfigurationProperties and validation is correct?
Consider a class annotated with @ConfigurationProperties(prefix = "mail") and fields annotated with @NotNull from javax.validation.constraints. Which statement is true about validation of these properties?
AValidation annotations are ignored unless @Validated is added to the class.
BValidation happens automatically without any extra annotation.
CValidation requires implementing Validator interface manually.
DValidation only works if properties are loaded from YAML, not properties files.
Attempts:
2 left
💡 Hint
Spring Boot requires an extra annotation to trigger validation on configuration properties.

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

  1. 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.
  2. Step 2: Compare with other options

    Options A, C, and D relate to other Spring Boot features but not to property binding.
  3. Final Answer:

    To bind external configuration properties to a Java class -> Option D
  4. 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

  1. Step 1: Identify how to activate configuration properties

    Spring Boot requires @EnableConfigurationProperties on a configuration or main class to enable binding of @ConfigurationProperties classes.
  2. 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.
  3. Final Answer:

    Add @EnableConfigurationProperties on a configuration class or main class -> Option B
  4. Quick Check:

    Enable config properties = D [OK]
Hint: Use @EnableConfigurationProperties to activate property binding [OK]
Common Mistakes:
  • Confusing @ComponentScan with enabling properties
  • Adding @RestController on properties class
  • Assuming @SpringBootApplication alone enables properties
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

  1. 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.
  2. Step 2: Check getter method output

    Calling getTimeout() returns the bound value 5000, so the printed output is 5000.
  3. Final Answer:

    5000 -> Option C
  4. 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

  1. Step 1: Check property binding requirements

    Spring Boot binds properties using setters. Without a setter for 'port', the value cannot be set.
  2. 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.
  3. Final Answer:

    Missing setter method for 'port' prevents binding -> Option A
  4. 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:
mail.server.host=smtp.example.com
mail.server.port=587
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

  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]
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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard