Bird
Raised Fist0
Spring Bootframework~10 mins

@Value for property injection in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - @Value for property injection
Start Spring Boot App
↓
Read application.properties
↓
Find @Value annotation
↓
Inject property value into field
↓
Use injected value in component
↓
App runs with configured values
Spring Boot reads properties, finds @Value annotations, injects values into fields, then uses them in the app.
Execution Sample
Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
  @Value("${app.message}")
  private String message;

  public String getMessage() {
    return message;
  }
}
This code injects the 'app.message' property value into the 'message' field of MyComponent.
Execution Table
StepActionProperty ReadField ValueResult
1Start Spring Boot app--App initializing
2Load application.propertiesapp.message=Hello Spring-Properties loaded
3Find @Value on 'message' field--Prepare to inject
4Inject 'app.message' valueHello Springmessage = 'Hello Spring'Field set
5Call getMessage()-message = 'Hello Spring'Returns 'Hello Spring'
6App runs using injected value-message = 'Hello Spring'App uses property value
7End--Injection complete
💡 Injection done after reading property and setting field; app uses injected value.
Variable Tracker
VariableStartAfter InjectionFinal
messagenull'Hello Spring''Hello Spring'
Key Moments - 3 Insights
Why is the 'message' field null before injection?
Before Spring injects the property value (see step 4 in execution_table), the field is null because it has not been set yet.
What happens if the property 'app.message' is missing?
Spring will fail to inject the value at step 4, causing an error unless a default value is provided in @Value or the property is defined.
Can @Value inject values into static fields?
No, @Value cannot inject into static fields because Spring injects instance fields during component creation (see step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'message' after step 4?
A'Hello Spring'
Bnull
C"${app.message}"
DEmpty string
💡 Hint
Check the 'Field Value' column at step 4 in execution_table.
At which step does Spring read the property from application.properties?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Property Read' column in execution_table.
If the property 'app.message' is missing, what will happen at step 4?
AField 'message' is set to null
BSpring throws an error
CField 'message' is set to empty string
DSpring ignores the field
💡 Hint
Refer to key_moments about missing property behavior.
Concept Snapshot
@Value("${property.name}") injects property values into fields.
Spring reads properties on startup.
Injection happens during component creation.
Fields must be non-static.
Missing properties cause errors unless defaults are set.
Full Transcript
When a Spring Boot app starts, it reads properties from application.properties. It looks for fields annotated with @Value and injects the matching property value into those fields. For example, if a field has @Value("${app.message}"), Spring finds the 'app.message' property and sets the field to that value. Before injection, the field is null. After injection, it holds the property value. If the property is missing, Spring throws an error unless a default is provided. This process happens during component creation, so static fields cannot be injected.

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

  1. Step 1: Understand the role of @Value

    @Value is used to inject configuration values like those from application.properties into Spring beans.
  2. Step 2: Compare with other options

    Defining beans, handling HTTP requests, and database connections are done by other annotations or classes, not @Value.
  3. Final Answer:

    To inject values from configuration files into Spring beans -> Option D
  4. 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

  1. Step 1: Recall correct syntax for property injection

    The property key must be inside ${} and the whole expression inside quotes: @Value("${property.key}").
  2. 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.
  3. Final Answer:

    @Value("${app.name}") private String appName; -> Option B
  4. 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

  1. 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.
  2. Step 2: Check default value of int field

    Since port is a primitive int and not injected, it defaults to 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Manual new means no injection, int defaults to 0 [OK]
Hint: Only Spring-managed beans get @Value injection [OK]
Common Mistakes:
  • Assuming @Value works on manually created objects
  • Expecting null for primitive int fields
  • Ignoring Spring bean lifecycle
4. What is wrong with this code snippet?
@Value("${missing.property}:defaultValue")
private String value;

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

  1. 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.
  2. Step 2: Identify the error cause

    If the colon is outside quotes or expression is malformed, Spring cannot parse it and throws an error.
  3. Final Answer:

    The default value syntax is incorrect; colon should be inside quotes -> Option C
  4. 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

  1. Step 1: Understand property injection for collections

    Spring does not automatically convert comma-separated strings to List<String> with plain @Value.
  2. 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.
  3. Final Answer:

    @Value("#{T(java.util.Arrays).asList('${app.modes}'.split(','))}") private List<String> modes; -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard