๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Spring @Value Annotation


Introduction

  • The @Value annotation in Spring is primarily used to inject the values into fields, method parameters or constructors.
  • The values which is going to be injected can come from :-
    • Property files (like application.properties or application.yml in Spring Boot)
    • Environment variables
    • System properties
    • Expressions or literal values (both are static values)
Syntax :

Below are some common ways to use the @Value annotation in Spring:

  • 1. Injecting from Property Files:
    @Value("${property.name}")
    private String propertyValue;
    Here, property.name is a key in application.properties or application.yml, and the value of this key will be injected.

  • 2. Injecting Environment Variables:
    @Value("${ENV_VAR_NAME}")
    private String envVariable;
    This will inject the value of the environment variable named ENV_VAR_NAME.

  • 3. Injecting System Properties:
    @Value("#{systemProperties['user.name']}")
    private String systemUser;
    This will inject the value of the system property user.name.

  • 4. Injecting Expressions (Dynamic Calculations):
    @Value("#{2 + 2}")
    private int calculatedValue;  // Injects 4


  • 5. Injecting Literal Values:
    @Value("Deepak")
    private String name;
    This will inject the literal string "Deepak" in name variable.

  • 6. Using @Value in Constructors:
    public MyClass(@Value("${property.name}") String propertyValue) {
        this.propertyValue = propertyValue;
    }
    This will inject the value of the system property user.name.

  • 7. Using @Value in Method Parameters:
    @Autowired
    public void setSomeValue(@Value("${property.name}") String propertyValue) {
        this.propertyValue = propertyValue;
    }
    This will inject the value of the system property user.name.

Basic Program:

Below is the program demonstrates how to set static value using @Value annotation.

Student.java
package in.sp.beans;

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

@Component
public class Student
{
	@Value("Deepak")
	private String name;
	
	@Value("101")
	private int rollno;
	
	@Value("deepak@gmail.com")
	private String emailid;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRollno() {
		return rollno;
	}
	public void setRollno(int rollno) {
		this.rollno = rollno;
	}
	public String getEmailid() {
		return emailid;
	}
	public void setEmailid(String emailid) {
		this.emailid = emailid;
	}
	
	public void display()
	{
		System.out.println("Name : "+name);
		System.out.println("Roll No : "+rollno);
		System.out.println("Email Id : "+emailid);
	}
}

MainApp.java
package in.sp.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import in.sp.beans.Student;

@Configuration
@ComponentScan(basePackages = "in.sp.beans")
public class MainApp 
{
	public static void main(String[] args)
	{
        // Spring container is created with MainApp as configuration class
		ApplicationContext context = new AnnotationConfigApplicationContext(MainApp.class);
		
        // Bean object is requested from Spring Container and stored in Student reference
		Student std = context.getBean(Student.class);

		std.display();
	}
}

Below is the output

Output:
Name : Deepak
Roll No : 101
Email Id : deepak@gmail.com


Points to Remember:
  • The @Value annotation is a powerful feature in Spring for injecting static or dynamic values from property files, environment variables, and SpEL expressions.
  • It supports usage in fields, constructors, setters, and method return values.
  • The annotation can handle defaults and expressions, making it flexible for multi-environment configurations.
  • To use @Value, the class must be a Spring-managed bean, which can be achieved through:
    • Stereotype annotations (@Component, @Service, @Repository, @Controller)
    • @Configuration with @Bean methods
    • XML configuration to define the bean