What is the purpose of the @Value annotation in Spring Boot?
The @Value annotation is used to inject values from property files or environment variables directly into Spring-managed beans.
Click to reveal answer
beginner
How do you inject a property named app.name from application.properties using @Value?
You use @Value("${app.name}") on a field or setter method to inject the value of app.name from the properties file.
Click to reveal answer
intermediate
Can @Value inject default values if the property is missing? How?
Yes. You can provide a default value using the syntax @Value("${property.name:defaultValue}"). If property.name is missing, defaultValue is used.
Click to reveal answer
beginner
Where can @Value be applied in a Spring bean?
@Value can be applied on fields, constructor parameters, or setter methods to inject property values.
Click to reveal answer
intermediate
What happens if you use @Value with a property that does not exist and no default value is provided?
Spring will throw an exception during application startup because it cannot resolve the property value.
Click to reveal answer
Which syntax correctly injects the property server.port using @Value?
A@Value("#server.port")
B@Value("server.port")
C@Value(server.port)
D@Value("${server.port}")
✗ Incorrect
The correct syntax uses ${property.name} inside quotes to inject the property value.
How do you provide a default value of 8080 for server.port if it is missing?
A@Value("${server.port:8080}")
B@Value("${server.port?8080}")
C@Value("${server.port|8080}")
D@Value("${server.port=8080}")
✗ Incorrect
The colon : syntax inside ${} provides a default value.
Where can you NOT use @Value in a Spring bean?
AOn local variables inside methods
BOn constructor parameters
COn setter methods
DOn private fields
✗ Incorrect
@Value cannot be used on local variables inside methods because Spring injects values during bean creation.
What will happen if a property used in @Value is missing and no default is set?
ASpring uses null as the value
BSpring ignores the missing property
CSpring throws an exception at startup
DSpring logs a warning but continues
✗ Incorrect
Spring fails to start because it cannot resolve the required property.
Which of these is a valid example of injecting a property with @Value?
A@Value("my.property") private String prop;
B@Value("${my.property}") private String prop;
C@Value(my.property) private String prop;
D@Value(#my.property) private String prop;
✗ Incorrect
Only option C uses the correct syntax with ${} and quotes.
Explain how to use @Value to inject a property from application.properties into a Spring bean field.
Think about how Spring reads properties and injects them.
You got /4 concepts.
Describe what happens if a property referenced by @Value is missing and no default value is provided.
Consider Spring's behavior when required config is missing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Value annotation in Spring Boot?
easy
A. To create database connections
B. To define a new Spring bean
C. To handle HTTP requests
D. To inject values from configuration files into Spring beans
Solution
Step 1: Understand the role of @Value
@Value is used to inject configuration values like those from application.properties into 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 D
Quick Check:
@Value injects config values = A [OK]
Hint: Remember: @Value pulls config values into your code [OK]
Common Mistakes:
Confusing @Value with @Bean or @Autowired
Thinking @Value handles HTTP or DB tasks
Ignoring that @Value reads from properties
2. Which of the following is the correct syntax to inject a property named app.name using @Value?
easy
A. @Value(${app.name}) private String appName;
B. @Value("${app.name}") private String appName;
C. @Value("app.name") private String appName;
D. @Value(app.name) private String appName;
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 B
Quick Check:
Correct syntax uses ${} inside quotes = D [OK]
Hint: Use @Value("${property.key}") with quotes and ${} [OK]
Common Mistakes:
Omitting quotes around the expression
Forgetting the ${} around the property key
Using plain property name without ${}
3. Given the property 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());
medium
A. 0
B. 8080
C. null
D. Compilation error
Solution
Step 1: Understand Spring bean lifecycle and injection
Property injection with @Value happens only when Spring creates and manages the bean. Here, ServerConfig is created manually with new, so injection does not happen.
Step 2: Check default value of int field
Since port is a primitive int and not injected, it defaults to 0.
Final Answer:
0 -> Option A
Quick Check:
Manual new means no injection, int defaults to 0 [OK]
Hint: Only Spring-managed beans get @Value injection [OK]
When missing.property is not defined in properties, the application fails to start with an error.
medium
A. The field must be public for @Value to work
B. The property key must be defined; default values are not supported
C. The default value syntax is incorrect; colon should be inside quotes
D. The @Value annotation cannot be used on String fields
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 C
Quick Check:
Default values must be inside ${} quotes [OK]
Hint: Put default after colon inside quotes in @Value [OK]
Common Mistakes:
Placing colon outside the quotes
Assuming default values are not supported
Making fields public unnecessarily
5. You want to inject a list of strings from a property app.modes=dev,prod,test into a Spring bean using @Value. Which approach correctly converts the property into a List<String>?
hard
A. @Value("#{T(java.util.Arrays).asList('${app.modes}'.split(','))}") private List<String> modes;
B. @Value("${app.modes}") private List<String> modes; // Spring auto-converts comma-separated string
C. @Value("${app.modes}") private String[] modes; // Use array instead of List
D. @Value("${app.modes}") private String modes; // Parse manually later
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 A
Quick Check:
Use SpEL split and asList for List injection [OK]
Hint: Use SpEL with split and asList to inject List<String> [OK]
Common Mistakes:
Expecting automatic conversion from String to List
Using array when List is required without conversion
Injecting as String and forgetting to parse
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.