Table of contents
1.
Introduction
2.
What is the @Value Annotation?
2.1.
Key Features of @Value Annotation:
3.
Spring @Value with Methods
3.1.
Basic Usage of @Value
3.2.
Using @Value with Methods
4.
How to Use the @Value Annotation?
5.
Implementation
5.1.
Prerequisites
5.2.
Example
5.2.1.
Step 1: Define application.properties
5.2.2.
Step 2: Use @Value in a Spring Component
5.2.3.
Step 3: Run the Spring Boot Application
6.
Spring @Value - Default Value
7.
Spring @Value - Spring Environment Property
8.
Spring @Value - System Environment
9.
Spring @Value - SpEL (Spring Expression Language)
10.
Limitations of @Value
10.1.
1. Limited to Simple Types
10.2.
2. No Support for Collections
10.3.
3. Limited to Injection Points
10.4.
Example of Limitations
11.
Frequently Asked Questions
11.1.
What is the purpose of @Value annotation in Spring Boot?
11.2.
How can I set a default value using @Value?
11.3.
Can I use @Value to inject values into method parameters?
12.
Conclusion
Last Updated: Feb 3, 2025
Easy

@Value Annotation in Spring Boot

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The @Value annotation in Spring Boot is a feature that allows developers to inject values into fields, methods, or constructors from property files, environment variables, or inline expressions. It is commonly used to configure application settings dynamically without hardcoding values in the source code. This helps in making applications more flexible and easier to manage.

@Value Annotation in Spring Boot

In this article, you will learn about the syntax of the @Value annotation, its key features, and how to use it effectively in your Spring Boot applications.

What is the @Value Annotation?

The @Value annotation in Spring Boot is used to assign values to variables in Spring components. It is commonly used to fetch values from application properties, environment variables, or default values. It is part of the Spring Framework and helps developers manage configurations dynamically.

Key Features of @Value Annotation:

  • Injects values into fields, method parameters, and constructors.
     
  • Supports default values if a property is not found.
     
  • Reads values from application properties and system environment variables.
     
  • Supports Spring Expression Language (SpEL) for dynamic values.

Spring @Value with Methods

The @Value annotation in Spring Boot is a powerful feature that allows you to inject values from your application properties file directly into your beans. This is particularly useful when you need to use configuration properties in your application. Let's look at how you can use @Value with methods.

Basic Usage of @Value

To use the @Value annotation, you first need to define your properties in the application.properties or application.yml file. For example, you might have a property like this in your application.properties file:

app.name=MySpringBootApplication
app.description=This is a Spring Boot application.
You can then inject these properties into your beans using the @Value annotation. Let’s see an example of how to do this:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppConfig {

    @Value("${app.name}")
    private String appName;

    @Value("${app.description}")
    private String appDescription;

    public void displayConfig() {
        System.out.println("App Name: " + appName);
        System.out.println("App Description: " + appDescription);
    }
}


In this example, the @Value annotation is used to inject the values of app.name and app.description from the application.properties file into the appName and appDescription fields, respectively. The displayConfig method then prints these values to the console.

Using @Value with Methods

You can also use the @Value annotation to inject values directly into method parameters. This can be useful when you want to pass configuration properties to a method. For example:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppConfig {
    public void displayConfig(@Value("${app.name}") String appName,
                              @Value("${app.description}") String appDescription) {
        System.out.println("App Name: " + appName);
        System.out.println("App Description: " + appDescription);
    }
}


In this example, the @Value annotation is used to inject the values of app.name and app.description directly into the displayConfig method parameters. This allows you to use the configuration properties within the method without needing to store them in fields.

How to Use the @Value Annotation?

To use the @Value annotation, ensure you have a Spring Boot application with a properly configured application.properties or application.yml file. The annotation can be used in Spring components like @Component, @Service, and @Controller.

Implementation

Prerequisites

Before implementing @Value, ensure you have:

  • Java 8+ installed
     
  • Spring Boot 2.x or later
     
  • Maven or Gradle for dependency management

Example

Step 1: Define application.properties

Create an application.properties file inside the src/main/resources directory:

app.message=Hello, Spring Boot!
app.port=8080

 

Step 2: Use @Value in a Spring Component

Create a Spring component and use @Value to inject values from application.properties:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppConfig {    
    @Value("${app.message}")
    private String message;
    @Value("${app.port}")
    private int port;
    public void displayConfig() {
        System.out.println("Message: " + message);
        System.out.println("Port: " + port);
    }
}

 

Step 3: Run the Spring Boot Application

Create a SpringBootApplication class to run the application:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootApplication
public class ValueAnnotationDemo implements CommandLineRunner {
    @Autowired
    private AppConfig appConfig;
    public static void main(String[] args) {
        SpringApplication.run(ValueAnnotationDemo.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        appConfig.displayConfig();
    }
}

 

Output

When you run the application, it will print:
Message: Hello, Spring Boot!
Port: 8080

Spring @Value - Default Value

Sometimes, a property may not exist in the configuration file. In such cases, @Value allows you to provide a default value:

@Value("${app.username:defaultUser}")
private String username;

 

If app.username is not found in application.properties, it assigns "defaultUser" as the value.

Spring @Value - Spring Environment Property

@Value can also fetch values from the Spring Environment dynamically:
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EnvConfig {
    
    @Autowired
    private Environment environment;
    
    public void printEnvProperty() {
        String appName = environment.getProperty("app.message");
        System.out.println("Application Message: " + appName);
    }
}

Spring @Value - System Environment

To inject system environment variables, use @Value with environment variables:

@Value("${JAVA_HOME}")
private String javaHome;


This will assign the JAVA_HOME system variable value to javaHome.

Spring @Value - SpEL (Spring Expression Language)

Spring Expression Language (SpEL) allows advanced property evaluation:

@Value("#{2 + 3}")
private int sum;
@Value("#{systemProperties['user.name']}")
private String systemUser;

 

Explanation:

  • #{2 + 3} assigns 5 to sum.
     
  • #{systemProperties['user.name']} fetches the current system user.

Limitations of @Value

While the @Value annotation in Spring Boot is incredibly useful for injecting configuration properties, it does come with some limitations. Understanding these limitations can help you use @Value more effectively and avoid potential pitfalls.

1. Limited to Simple Types

The @Value annotation is primarily designed to inject simple types such as strings, integers, and booleans. If you need to inject more complex objects, you might encounter issues. For example, if you have a configuration class that you want to inject, @Value won't be able to handle that directly. Instead, you would need to use the @ConfigurationProperties annotation for such cases.

2. No Support for Collections

Another limitation is that @Value does not support injecting collections directly. If you have a list or map of configuration properties, you cannot use @Value to inject them. Instead, you would need to use the @ConfigurationProperties annotation or manually parse the properties.

3. Limited to Injection Points

The @Value annotation can only be used on fields, constructors, and setter methods. It cannot be used on arbitrary methods or parameters. This means that if you want to inject a value into a method that is not a constructor or setter, you won't be able to use @Value.

Example of Limitations

Let's discuss these limitations with an example. Suppose you have a configuration property that is a list of strings:

app.features=feature1,feature2,feature3
If you try to inject this list using @Value, you will encounter issues:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppConfig {

    @Value("${app.features}")
    private List<String> features; // This will not work

    public void displayFeatures() {
        System.out.println("App Features: " + features);
    }
}


In this example, the @Value annotation will not be able to inject the list of features directly. Instead, you would need to use @ConfigurationProperties or manually parse the string into a list.

Frequently Asked Questions

What is the purpose of @Value annotation in Spring Boot?

The @Value annotation is used to inject values into Spring Beans from application properties, system environment, and SpEL expressions.

How can I set a default value using @Value?

You can provide a default value using : inside the annotation. Example- @Value("${app.username:defaultUser}").

Can I use @Value to inject values into method parameters?

Yes, you can use @Value to inject values into method parameters.

Conclusion

The @Value annotation in Spring Boot simplifies value injection from properties, environment variables, and expressions. It is a powerful tool for managing configurations dynamically. By using @Value, developers can create more flexible and maintainable Spring Boot applications. 

Live masterclass