@ConfigurationProperties helps you read settings from files like application.properties or application.yml into Java objects. This makes it easy and safe to use configuration values in your code without mistakes.
@ConfigurationProperties for type-safe config in Spring Boot
@ConfigurationProperties(prefix = "your.prefix") public class YourConfig { private String property; // getters and setters }
The prefix matches the start of property names in your config files.
You must add @EnableConfigurationProperties or use @ConfigurationPropertiesScan to activate binding.
@ConfigurationProperties(prefix = "app") public class AppProperties { private String name; private int timeout; // getters and setters }
@ConfigurationProperties(prefix = "server") public class ServerProperties { private int port; private Security security; public static class Security { private boolean enabled; // getters and setters } // getters and setters }
This example shows a simple config class that reads app.name and app.timeout from application.properties. Spring Boot automatically fills the fields when the app starts.
package com.example.demo.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "app") public class AppConfig { private String name; private int timeout; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } } // In application.properties: // app.name=MyApp // app.timeout=30 // Usage in another component: // @Autowired // private AppConfig appConfig; // System.out.println(appConfig.getName()); // prints MyApp // System.out.println(appConfig.getTimeout()); // prints 30
Make sure to add @Component or register the config class as a bean so Spring can create it.
Use standard getters and setters for Spring to bind properties correctly.
You can validate properties by adding @Validated and validation annotations like @NotNull.
@ConfigurationProperties lets you map config files to Java classes easily.
It groups related settings and reduces errors from manual property reading.
Works well with nested properties and supports validation.
Practice
@ConfigurationProperties in a Spring Boot application?Solution
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.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.Final Answer:
To bind external configuration properties to a Java class for type-safe access -> Option BQuick Check:
@ConfigurationProperties = type-safe config binding [OK]
- Confusing @ConfigurationProperties with REST controllers
- Thinking it creates database entities
- Assuming it handles HTTP requests
@ConfigurationProperties support in a Spring Boot application?Solution
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.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.Final Answer:
Annotate the class with @Component or use @EnableConfigurationProperties on a configuration class -> Option CQuick Check:
Enable with @Component or @EnableConfigurationProperties [OK]
- Using @Entity or @RestController instead of @Component
- Forgetting to register the properties class as a bean
- Assuming @Service enables configuration binding
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; }
}Solution
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.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.Final Answer:
5000 -> Option AQuick Check:
Property value binds correctly = 5000 [OK]
- Assuming default 0 because of missing @Value
- Confusing null with primitive int default
- Expecting exception without validation
@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: notANumberWhat will happen when Spring Boot starts?
Solution
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.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.Final Answer:
Application fails to start with a binding exception due to invalid port value -> Option DQuick Check:
Invalid type causes startup failure [OK]
- Assuming default values are used silently
- Expecting warnings instead of errors
- Thinking string can be assigned to int field
app:
security:
enabled: true
roles:
- USER
- ADMIN
Which of the following Java class structures correctly uses @ConfigurationProperties to map this nested config?Solution
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.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.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.Final Answer:
Option A with nested Security class inside AppProperties and prefix 'app' -> Option AQuick Check:
Nested class + prefix 'app' = correct mapping [OK]
- Using wrong prefix for nested properties
- Flattening nested properties into one class
- Not annotating main class with @ConfigurationProperties
Ready to prove it?
Pass the test and this pattern moves unknown
