
Introduction to @Value Annotation
The @Value
annotation in Spring Boot is used for injecting values into fields in a Spring-managed bean. It supports retrieving values from application properties files, environment variables, or even expressions. This is particularly useful for configuring application settings and externalizing configuration.
Basic Usage of @Value
Injecting a Property Value
To inject a value from the application.properties
file, you can use the @Value
annotation. Here’s an example:
application.properties:
properties
app.name=My Spring Boot Application
MyService.java:
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service public class MyService { @Value("${app.name}") private String appName; public void printAppName() { System.out.println("Application Name: " + appName); } }
In this example, the value of app.name
from the application.properties
file is injected into the appName
field.
Default Values
You can also provide a default value in case the property is not defined:
MyService.java:
java
Copy code
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service public class MyService { @Value("${app.description:Default Application Description}") private String appDescription; public void printAppDescription() { System.out.println("Application Description: " + appDescription); } }
Here, if app.description
is not defined in the properties file, the default value “Default Application Description” will be used.
Advanced Usage of @Value
Injecting System Properties and Environment Variables
You can inject system properties and environment variables using the @Value
annotation:
MyService.java:
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service public class MyService { @Value("${user.home}") private String userHome; @Value("${JAVA_HOME}") private String javaHome; public void printSystemProperties() { System.out.println("User Home: " + userHome); System.out.println("Java Home: " + javaHome); } }
Using SpEL (Spring Expression Language)
The @Value
annotation supports SpEL expressions for more complex scenarios:
MyService.java:
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service public class MyService { @Value("#{systemProperties['user.name']}") private String userName; @Value("#{T(java.lang.Math).random() * 100.0}") private double randomValue; public void printSpelValues() { System.out.println("User Name: " + userName); System.out.println("Random Value: " + randomValue); } }
Injecting List or Map Values
You can also inject lists or maps from properties:
application.properties:
properties
app.servers=server1,server2,server3
app.settings.key1=value1
app.settings.key2=value2
MyService.java:
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List; import java.util.Map;@Service public class MyService { @Value("#{'${app.servers}'.split(',')}") private List<String> servers; @Value("#{${app.settings}}") private Map<String, String> settings; public void printListsAndMaps() { System.out.println("Servers: " + servers); System.out.println("Settings: " + settings); } }
Conclusion
The @Value
annotation is a powerful tool in Spring Boot for injecting configuration values into your beans. Whether you need to inject simple properties, system properties, environment variables, or more complex SpEL expressions, @Value
makes it easy to externalize configuration and keep your application flexible and maintainable. The Spring Boot @Value
annotation streamlines the process, ensuring that your configuration management is efficient and straightforward.