Challenge - 5 Problems
Custom Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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());
Attempts:
2 left
💡 Hint
Remember that Spring Boot automatically binds properties with matching prefix to the fields in the class annotated with @ConfigurationProperties.
✗ Incorrect
The property 'app.title' in application.properties is bound to the 'title' field in the AppProperties class because of the prefix 'app'. Spring Boot automatically injects the value 'MyApp' into the field, so calling getTitle() returns 'MyApp'.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
Use the annotation that explicitly enables configuration properties classes.
✗ Incorrect
The @EnableConfigurationProperties annotation with the class as parameter tells Spring Boot to create a bean for that class and bind properties with the specified prefix. Other options either scan components or enable auto-configuration but do not specifically enable configuration properties binding.
🔧 Debug
advanced2: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
}Attempts:
2 left
💡 Hint
Spring Boot uses setters to inject property values.
✗ Incorrect
Spring Boot uses JavaBean conventions to bind properties. Without a setter method for 'port', Spring cannot set the value, so the field remains at its default (0). The prefix 'server' is correct, and @Component is valid. The field does not need to be public if there is a setter.
❓ state_output
advanced2: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; }
} Attempts:
2 left
💡 Hint
YAML lists map to Java List fields with matching names.
✗ Incorrect
The YAML list under 'app.servers' binds to the List servers field in AppProperties. The list contains two strings as defined, so getServers() returns a list with those two hostnames.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Spring Boot requires an extra annotation to trigger validation on configuration properties.
✗ Incorrect
To enable validation of @ConfigurationProperties fields using javax.validation annotations, the class must also be annotated with @Validated. Without it, validation annotations are ignored. Validation does not depend on the source format and does not require manual Validator implementation.