What if you could change app settings without touching a single line of code?
Why @Value for property injection in Spring Boot? - Purpose & Use Cases
Imagine you have many settings like database URLs, API keys, or feature flags hardcoded inside your Java classes.
Every time you want to change a setting, you must find the code, edit it, and recompile your app.
This manual way is slow and risky because you might forget to update all places or accidentally break something.
It also makes your app less flexible and harder to configure for different environments like testing or production.
The @Value annotation lets you inject values from external property files directly into your Spring components.
This means you can change settings without touching the code, just by editing a simple file.
private String apiUrl = "https://old-api.com";@Value("${api.url}")
private String apiUrl;You can easily manage and change configuration values outside your code, making your app flexible and environment-friendly.
For example, you can have one API URL for your local machine and a different one for your live server, switching automatically without code changes.
Hardcoding settings makes apps rigid and error-prone.
@Value injects external properties into your code cleanly.
This improves flexibility, safety, and ease of configuration.
Practice
@Value annotation in Spring Boot?Solution
Step 1: Understand the role of
@Value@Valueis used to inject configuration values like those fromapplication.propertiesinto Spring beans.Step 2: Compare with other options
Defining beans, handling HTTP requests, and database connections are done by other annotations or classes, not@Value.Final Answer:
To inject values from configuration files into Spring beans -> Option DQuick Check:
@Value injects config values = A [OK]
- Confusing @Value with @Bean or @Autowired
- Thinking @Value handles HTTP or DB tasks
- Ignoring that @Value reads from properties
app.name using @Value?Solution
Step 1: Recall correct syntax for property injection
The property key must be inside ${} and the whole expression inside quotes:@Value("${property.key}").Step 2: Check each option
@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.Final Answer:
@Value("${app.name}") private String appName; -> Option BQuick Check:
Correct syntax uses ${} inside quotes = D [OK]
- Omitting quotes around the expression
- Forgetting the ${} around the property key
- Using plain property name without ${}
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());Solution
Step 1: Understand Spring bean lifecycle and injection
Property injection with@Valuehappens only when Spring creates and manages the bean. Here,ServerConfigis created manually withnew, so injection does not happen.Step 2: Check default value of int field
Sinceportis a primitive int and not injected, it defaults to 0.Final Answer:
0 -> Option AQuick Check:
Manual new means no injection, int defaults to 0 [OK]
- Assuming @Value works on manually created objects
- Expecting null for primitive int fields
- Ignoring Spring bean lifecycle
@Value("${missing.property}:defaultValue")
private String value;When
missing.property is not defined in properties, the application fails to start with an error.Solution
Step 1: Check default value syntax in @Value
The default value must be inside the quotes as part of the expression:@Value("${property:default}")is correct.Step 2: Identify the error cause
If the colon is outside quotes or expression is malformed, Spring cannot parse it and throws an error.Final Answer:
The default value syntax is incorrect; colon should be inside quotes -> Option CQuick Check:
Default values must be inside ${} quotes [OK]
- Placing colon outside the quotes
- Assuming default values are not supported
- Making fields public unnecessarily
app.modes=dev,prod,test into a Spring bean using @Value. Which approach correctly converts the property into a List<String>?Solution
Step 1: Understand property injection for collections
Spring does not automatically convert comma-separated strings to List<String> with plain @Value.Step 2: Use SpEL expression for conversion
@Value("#{T(java.util.Arrays).asList('${app.modes}'.split(','))}") private List<String> modes; uses Spring Expression Language (SpEL) to split the string and convert it to a List<String>, which is the correct approach.Final Answer:
@Value("#{T(java.util.Arrays).asList('${app.modes}'.split(','))}") private List<String> modes; -> Option AQuick Check:
Use SpEL split and asList for List injection [OK]
- Expecting automatic conversion from String to List
- Using array when List is required without conversion
- Injecting as String and forgetting to parse
Ready to prove it?
Pass the test and this pattern moves unknown
