In Spring, the @Value
annotation allows you to inject values from properties files or environment variables into your application. Here’s how you can use it step by step:
1. Add a properties
or file to your project yaml
Create a properties file, such as application.properties
or application.yml
, and add your key-value configurations.
Example (application.properties
):
app.name=My Application
app.version=1.0.0
app.description=A demo application using @Value
Example (application.yml
):
app:
name: My Application
version: 1.0.0
description: A demo application using @Value
2. Enable property loading in your Spring application
Spring Boot automatically loads application.properties
or application.yml
when running your application. You don’t need any additional configuration for this if you’re using Spring Boot.
For a non-Boot Spring application, ensure you configure the @PropertySource
annotation. For example:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {
}
3. Inject property values using @Value
Add the @Value
annotation to a field, constructor parameter, or setter to inject property values.
Example 1: Field Injection
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppProperties {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
@Value("${app.description}")
private String appDescription;
public void printProperties() {
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
System.out.println("App Description: " + appDescription);
}
}
Example 2: Constructor Injection
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppProperties {
private final String appName;
private final String appVersion;
public AppProperties(
@Value("${app.name}") String appName,
@Value("${app.version}") String appVersion
) {
this.appName = appName;
this.appVersion = appVersion;
}
public void printProperties() {
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
Example 3: Default Values
You can provide default values in case a specified property is not present:
@Value("${app.unknown:Default Value}")
private String unknownProperty;
4. Running the application
If you run a Spring Boot application with the above configuration, the values defined in application.properties
(or application.yml
) will be injected into the variables annotated with @Value
.
5. Notes and Best Practices
- Use
@ConfigurationProperties
for Groups of Properties: When working with many properties, consider using@ConfigurationProperties
instead of@Value
. It is more organized and allows you to map properties to a dedicated class.- Example:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "app") public class AppConfigProps { private String name; private String version; private String description; // Getters and setters }
- Placeholders for Environment Variables: You can use
${ENVIRONMENT_VAR}
placeholders to inject environment-specific variables. - Property Validation (Optional): For strict validation of the presence of properties, consider combining
@Value
with property validation tools like Hibernate Validator or custom logic.
Maven Dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.6</version>
</dependency>