Bird
Raised Fist0
Spring Bootframework~20 mins

@ConfigurationProperties for type-safe config in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Boot 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 binding properties with @ConfigurationProperties?
Given the following Spring Boot configuration class and application.properties, what will be the value of appConfig.getTimeout() after the application starts?

AppConfig.java:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private int timeout;
    public int getTimeout() { return timeout; }
    public void setTimeout(int timeout) { this.timeout = timeout; }
}

application.properties:
app.timeout=5000
Spring Boot
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private int timeout;
    public int getTimeout() { return timeout; }
    public void setTimeout(int timeout) { this.timeout = timeout; }
}
Anull
B0
CThrows a runtime exception
D5000
Attempts:
2 left
💡 Hint
Check how the prefix in @ConfigurationProperties matches the property keys.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a nested property class for @ConfigurationProperties?
You want to bind nested properties under prefix 'server' like server.address.host=localhost and server.address.port=8080. Which of the following nested class definitions inside a @ConfigurationProperties class is syntactically correct?
Spring Boot
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "server")
public class ServerConfig {
    private Address address = new Address();
    public Address getAddress() { return address; }
    public void setAddress(Address address) { this.address = address; }

    public static class Address {
        private String host;
        private int port;
        // getters and setters
    }
}
Apublic static class Address { private String host; private int port; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
Bpublic class Address { public String host; public int port; }
Cstatic class Address { private String host; private int port; }
Dpublic static class Address { private String host; private int port; }
Attempts:
2 left
💡 Hint
Remember that nested classes for binding need proper getters and setters.
🔧 Debug
advanced
2:00remaining
Why does this @ConfigurationProperties class fail to bind properties?
Consider this class:
@Component
@ConfigurationProperties(prefix = "mail")
public class MailConfig {
    private String host;
    private int port;
}

Given mail.host=smtp.example.com and mail.port=25 in application.properties, why are the properties not bound?
Spring Boot
@Component
@ConfigurationProperties(prefix = "mail")
public class MailConfig {
    private String host;
    private int port;
}
AFields are private but missing getters and setters, so Spring Boot cannot bind them.
BThe prefix 'mail' does not match the property keys.
CThe class is missing @EnableConfigurationProperties annotation.
DThe fields should be static for binding to work.
Attempts:
2 left
💡 Hint
Check how Spring Boot binds properties to fields.
🧠 Conceptual
advanced
2:00remaining
What is the effect of using @ConstructorBinding with @ConfigurationProperties?
When you annotate a class with @ConfigurationProperties and @ConstructorBinding, what changes in how Spring Boot binds properties?
AIt disables binding and requires manual property injection.
BProperties are bound only if setters are present.
CSpring Boot uses the constructor parameters to bind properties instead of setters.
DBinding happens only for static fields.
Attempts:
2 left
💡 Hint
Think about immutable configuration classes.
❓ state_output
expert
2:00remaining
What is the value of 'enabled' after binding with relaxed binding rules?
Given this configuration class:
@Component
@ConfigurationProperties(prefix = "feature")
public class FeatureConfig {
    private boolean enabled;
    public boolean isEnabled() { return enabled; }
    public void setEnabled(boolean enabled) { this.enabled = enabled; }
}

And this property in application.properties:
feature.ENABLED=true

What will be the value of featureConfig.isEnabled() after startup?
Spring Boot
@Component
@ConfigurationProperties(prefix = "feature")
public class FeatureConfig {
    private boolean enabled;
    public boolean isEnabled() { return enabled; }
    public void setEnabled(boolean enabled) { this.enabled = enabled; }
}
Afalse
Btrue
Cnull
DThrows a binding exception
Attempts:
2 left
💡 Hint
Spring Boot property binding is case-insensitive and supports relaxed binding.

Practice

(1/5)
1. What is the main purpose of using @ConfigurationProperties in a Spring Boot application?
easy
A. To create REST endpoints automatically
B. To bind external configuration properties to a Java class for type-safe access
C. To define database entities
D. To handle HTTP requests and responses

Solution

  1. Step 1: Understand the role of @ConfigurationProperties

    This annotation is used to map properties from configuration files (like application.properties or application.yml) into Java classes.
  2. Step 2: Identify the benefit of type-safe access

    By binding properties to a class, you avoid manual parsing and get compile-time checking of property names and types.
  3. Final Answer:

    To bind external configuration properties to a Java class for type-safe access -> Option B
  4. Quick Check:

    @ConfigurationProperties = type-safe config binding [OK]
Hint: Remember: @ConfigurationProperties maps config to Java class [OK]
Common Mistakes:
  • Confusing @ConfigurationProperties with REST controllers
  • Thinking it creates database entities
  • Assuming it handles HTTP requests
2. Which of the following is the correct way to enable @ConfigurationProperties support in a Spring Boot application?
easy
A. Add @RestController on the configuration class
B. Annotate the class with @Entity
C. Annotate the class with @Component or use @EnableConfigurationProperties on a configuration class
D. Use @Service annotation on the properties class

Solution

  1. Step 1: Identify how Spring Boot detects @ConfigurationProperties classes

    Spring Boot requires the class to be a Spring bean, which can be done by annotating it with @Component or by enabling it via @EnableConfigurationProperties.
  2. Step 2: Recognize incorrect annotations

    @Entity is for JPA entities, @RestController is for web controllers, and @Service is for service layer beans, none of which enable configuration properties binding.
  3. Final Answer:

    Annotate the class with @Component or use @EnableConfigurationProperties on a configuration class -> Option C
  4. Quick Check:

    Enable with @Component or @EnableConfigurationProperties [OK]
Hint: Use @Component or @EnableConfigurationProperties to activate binding [OK]
Common Mistakes:
  • Using @Entity or @RestController instead of @Component
  • Forgetting to register the properties class as a bean
  • Assuming @Service enables configuration binding
3. Given the following configuration class and properties, what will be the value of 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. 5000
B. 0
C. null
D. Throws an exception

Solution

  1. Step 1: Match property prefix and field

    The prefix 'app' matches the class prefix, and 'timeout' matches the field name, so the value 5000 binds correctly.
  2. Step 2: Understand default value and binding

    Since the property is set in the YAML, the field will be set to 5000, not the default 0 or null.
  3. Final Answer:

    5000 -> Option A
  4. Quick Check:

    Property value binds correctly = 5000 [OK]
Hint: Match prefix and field names exactly for binding [OK]
Common Mistakes:
  • Assuming default 0 because of missing @Value
  • Confusing null with primitive int default
  • Expecting exception without validation
4. Consider this configuration class:
@ConfigurationProperties(prefix = "server")
public class ServerConfig {
    private String host;
    private int port;

    public String getHost() { return host; }
    public void setHost(String host) { this.host = host; }

    public int getPort() { return port; }
    public void setPort(int port) { this.port = port; }
}
If the application.yml contains:
server:
  host: localhost
  port: notANumber
What will happen when Spring Boot starts?
medium
A. Port is set to 0 by default, application starts normally
B. Application starts, port is set to string 'notANumber'
C. Port is ignored, host is set, application starts with warning
D. Application fails to start with a binding exception due to invalid port value

Solution

  1. Step 1: Analyze property type mismatch

    The 'port' field is an int, but the YAML provides a non-numeric string 'notANumber', causing a type mismatch.
  2. Step 2: Understand Spring Boot binding behavior

    Spring Boot throws a binding exception on startup if it cannot convert the property to the required type.
  3. Final Answer:

    Application fails to start with a binding exception due to invalid port value -> Option D
  4. Quick Check:

    Invalid type causes startup failure [OK]
Hint: Type mismatch in config causes startup failure [OK]
Common Mistakes:
  • Assuming default values are used silently
  • Expecting warnings instead of errors
  • Thinking string can be assigned to int field
5. You want to create a nested configuration properties class to map this YAML:
app:
  security:
    enabled: true
    roles:
      - USER
      - ADMIN
Which of the following Java class structures correctly uses @ConfigurationProperties to map this nested config?
hard
A. public class AppProperties { private Security security; public static class Security { private boolean enabled; private List<String> roles; // getters and setters } // getters and setters } @ConfigurationProperties(prefix = "app") @Component public class AppProperties {}
B. @ConfigurationProperties(prefix = "app.security") @Component public class Security { private boolean enabled; private List<String> roles; // getters and setters }
C. @ConfigurationProperties(prefix = "app") @Component public class AppProperties { private boolean enabled; private List<String> roles; // getters and setters }
D. @ConfigurationProperties(prefix = "security") @Component public class Security { private boolean enabled; private List<String> roles; // getters and setters }

Solution

  1. Step 1: Understand nested property mapping

    To map nested properties, create a nested class inside the main properties class and define fields matching the nested keys.
  2. Step 2: Match prefix and class structure

    The prefix 'app' matches the root, and the nested Security class maps the 'security' subtree with 'enabled' and 'roles'. The main class must be annotated with @ConfigurationProperties and registered as a bean.
  3. Step 3: Evaluate options

    public class AppProperties { private Security security; public static class Security { private boolean enabled; private List<String> roles; // getters and setters } // getters and setters } @ConfigurationProperties(prefix = "app") @Component public class AppProperties {} correctly nests Security inside AppProperties and uses prefix 'app'. @ConfigurationProperties(prefix = "app.security") @Component public class Security { private boolean enabled; private List<String> roles; // getters and setters } uses prefix 'app.security' but does not nest. @ConfigurationProperties(prefix = "app") @Component public class AppProperties { private boolean enabled; private List<String> roles; // getters and setters } flattens properties incorrectly. @ConfigurationProperties(prefix = "security") @Component public class Security { private boolean enabled; private List<String> roles; // getters and setters } uses wrong prefix.
  4. Final Answer:

    Option A with nested Security class inside AppProperties and prefix 'app' -> Option A
  5. Quick Check:

    Nested class + prefix 'app' = correct mapping [OK]
Hint: Use nested classes for nested config with root prefix [OK]
Common Mistakes:
  • Using wrong prefix for nested properties
  • Flattening nested properties into one class
  • Not annotating main class with @ConfigurationProperties
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