Performance: @Value for property injection
This affects application startup time and memory usage by injecting configuration values into Spring beans.
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "my") public class MyProperties { private String property; public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } }
public class MyService { @Value("${my.property}") private String myProperty; public String getProperty() { return myProperty; } }
| Pattern | Reflection Calls | Startup Time Impact | Runtime Cost | Verdict |
|---|---|---|---|---|
| Multiple @Value annotations | High (one per field) | Medium (more reflection calls) | Negligible | [!] OK |
| @ConfigurationProperties binding | Low (batched binding) | Low (faster startup) | Negligible | [OK] Good |
@Value annotation in Spring Boot?@Value@Value is used to inject configuration values like those from application.properties into Spring beans.@Value.app.name using @Value?@Value("${property.key}").@Value("${app.name}") private String appName; matches the correct syntax. The other options miss either the quotes around the expression, the ${} delimiters, or use quotes around just the property name.server.port=8080 in application.properties, what will be the output of this Spring component?public class ServerConfig {
@Value("${server.port}")
private int port;
public int getPort() {
return port;
}
}
// In main method:
ServerConfig config = new ServerConfig();
System.out.println(config.getPort());@Value happens only when Spring creates and manages the bean. Here, ServerConfig is created manually with new, so injection does not happen.port is a primitive int and not injected, it defaults to 0.@Value("${missing.property}:defaultValue")
private String value;missing.property is not defined in properties, the application fails to start with an error.@Value("${property:default}") is correct.app.modes=dev,prod,test into a Spring bean using @Value. Which approach correctly converts the property into a List<String>?Pass the test and this pattern moves unknown