Performance: Custom configuration properties
This affects application startup time and memory usage by how configuration properties are loaded and bound.
Using a single @ConfigurationProperties class to bind all related properties: @ConfigurationProperties(prefix = "app") public class AppProperties { private int timeout; private String url; // getters and setters } @Configuration @EnableConfigurationProperties(AppProperties.class) public class MyService { private final AppProperties props; public MyService(AppProperties props) { this.props = props; } }
Using @Value annotations repeatedly for many properties scattered across classes: @Component public class MyService { @Value("${app.timeout}") private int timeout; @Value("${app.url}") private String url; // many more @Value injections }
| Pattern | Property Lookups | Startup Delay | Memory Usage | Verdict |
|---|---|---|---|---|
| Multiple @Value annotations | Many repeated lookups | Higher (tens of ms) | Higher due to scattered bindings | [X] Bad |
| Single @ConfigurationProperties class | Single batch lookup | Lower (few ms) | Lower due to structured binding | [OK] Good |
@ConfigurationProperties in Spring Boot?@ConfigurationPropertiesapplication.properties or application.yml) to fields in a Java class.@ConfigurationProperties support in a Spring Boot application?@EnableConfigurationProperties on a configuration or main class to enable binding of @ConfigurationProperties classes.@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.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; }
}app.timeout property in YAML is set to 5000. The class with @ConfigurationProperties(prefix = "app") binds this value to the timeout field.getTimeout() returns the bound value 5000, so the printed output is 5000.@ConfigurationProperties(prefix = "server")
public class ServerProperties {
private String port;
public String getPort() { return port; }
// Missing setter method
}application.properties like this:
mail.server.host=smtp.example.com mail.server.port=587Which of the following Java classes correctly models this nested property using
@ConfigurationProperties?mail.server.host require a class with prefix mail and a nested class Server to hold host and port.@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.Pass the test and this pattern moves unknown