How to Use @Value Annotation for Properties in Spring
Use the
@Value annotation in Spring to inject values from properties files or environment variables directly into fields, methods, or constructor parameters. Place @Value("${property.name}") above the target to bind the property value at runtime.Syntax
The @Value annotation uses the syntax @Value("${property.key:defaultValue}"). Here, property.key is the name of the property you want to inject, and defaultValue is optional, used if the property is missing.
- @Value: Annotation to inject property values.
- ${...}: Placeholder syntax to refer to property keys.
- defaultValue: Optional fallback if the property is not found.
java
@Value("${property.key:defaultValue}") private String someField;
Example
This example shows how to inject a property value from application.properties into a Spring component field using @Value. It prints the injected value when the application runs.
java
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${app.message:Hello, default message!}") private String message; public void printMessage() { System.out.println("Injected message: " + message); } } // In application.properties file: // app.message=Welcome to Spring @Value example! // Main application to run: import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class DemoApplication implements CommandLineRunner { private final MyComponent myComponent; public DemoApplication(MyComponent myComponent) { this.myComponent = myComponent; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) { myComponent.printMessage(); } }
Output
Injected message: Welcome to Spring @Value example!
Common Pitfalls
Common mistakes when using @Value include:
- Forgetting the
${}syntax around the property key. - Not providing a default value, causing errors if the property is missing.
- Using
@Valueon static fields (which is not supported). - Injecting complex types without proper conversion.
Always ensure your properties file is loaded and the keys match exactly.
java
/* Wrong usage: missing ${} */ @Value("app.message") private String message; /* Correct usage: with ${} and optional default */ @Value("${app.message:Default message}") private String message;
Quick Reference
- Use
@Value("${property.key}")to inject simple property values. - Provide a default with
:defaultValueto avoid errors. - Do not use
@Valueon static fields. - Works with fields, constructor parameters, and methods.
- Properties come from
application.properties,application.yml, or environment variables.
Key Takeaways
Use @Value with ${property.key} syntax to inject property values in Spring beans.
Always provide a default value to prevent errors if the property is missing.
Do not apply @Value to static fields as injection won't work.
Properties can come from application.properties, YAML files, or environment variables.
@Value works on fields, constructors, and setter methods for flexible injection.