Spring @Value Annotation with Example

One of the most important annotations in spring is @Value annotation which is used to assign default values ​​to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL). It is generally used for injecting values ​​into configuration variables, which we will show and explain in the following example.

Implementation: Project

Prerequisites:

Step 1: First, let’s create a simple Spring Application and inject the literal values ​​by setter injection. So, create a simple class Student having three attributes rollNo, name, and age. Create setter methods for these two attributes and a simple method to print the details of the student.

File: Student.java

Java

 

public class Student {

 

    

    private int rollNo;

    private String name;

    private int age;

 

    

    public void setRollNo(int rollNo)

    {

        

        this.rollNo = rollNo;

    }

 

    

    public void setName(String name) { this.name = name; }

 

    

    public void setAge(int age) { this.age = age; }

 

    

    public void display()

    {

        

        System.out.println("Roll No: " + rollNo);

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

    }

}

Step 2: Let’s create a properties file in your classpath and name the file as student-info.properties (for this example we name it like this, you can name it according to your need). And in this file, we are going to write something like as follows:

student.rollNo = 101
student.name = Sagar
student.age = 20

Step 3: Now let’s create a Student Bean in the beans.xml file and inside the bean, you have to add your property’s name and its corresponding values ​​inside the tag. For example, for this project, we can write something like this

<context:property-placeholder location="classpath:student-info.properties"/>

<bean id="student" class="Student">
   <property name="rollNo" value="${student.rollNo}"/>
   <property name="name" value="${student.name}"/>
   <property name="age" value="${student.age}"/>
</bean>

File: beans.xml

Step 4: So now our bean is ready. Now let’s create a class and define the main() method inside that class. Suppose we have created a class named Main and we have defined the main() method inside this class.

File: Main.java

Java

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Main {

 

    

    public static void main(String[] args)

    {

        

        ApplicationContext context

            = new ClassPathXmlApplicationContext(

                "beans.xml");

 

        

        Student student

            = context.getBean("student", Student.class);

 

        

        

        student.display();

    }

}

Step 5: Now run your main() method and the output will be like this.

Output:

Roll No: 101
Name: Sagar
Age: 20

Code Explanation: So the application is working fine. Now come to the beans.xml file again. And in this file, we don’t want to set value like this as we have done. We want to use some annotation for doing the same thing. And here @Value Annotation comes into the picture. So we can modify our Student.java file something like this

File: Student.java

Java

 

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

 

public class Student {

 

    

    private int rollNo;

    private String name;

    private int age;

 

    @Value("101")

    

    public void setRollNo(int rollNo)

    {

        this.rollNo = rollNo;

    }

 

    @Value("Anshul")

    

    public void setName(String name)

    {

        this.name = name;

    }

 

    @Value("25")

    

    public void setAge(int age)

    {

        this.age = age;

    }

 

    

    public void display()

    {

        

        System.out.println("Roll No: " + rollNo);

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

    }

}

Step 6: dd the following line inside your beans.xml file

<context:annotation-config/>

File: beans.xml

Step 7: Run your main() method and the output will be like this.

Output:

Roll No: 101
Name: Anshul
Age: 25

Note: Or you can also set the values ​​dynamically from the properties file. We can modify our Student.java file something like this

File: Student.java

Java

 

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

 

public class Student {

 

    

    private int rollNo;

    private String name;

    private int age;

 

    @Value("${student.rollNo}")

    

    public void setRollNo(int rollNo)

    {

 

        

        this.rollNo = rollNo;

    }

 

    @Value("${student.name}")

    

    public void setName(String name)

    {

        this.name = name;

    }

 

    @Value("${student.age}")

    

    public void setAge(int age)

    {

        this.age = age;

    }

 

    

    public void display()

    {

        

        System.out.println("Roll No: " + rollNo);

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

    }

}

Tip: Don’t forget to add the below line inside your beans.xml file

<context:property-placeholder location="classpath:student-info.properties"/>

File: beans.xml

Run your main() method and the output will be like this.

Output:

Roll No: 101
Name: Sagar
Age: 20

Now let’s discuss one more interesting concept on Spring @Value Annotation. We can also use the @Value annotation before the fields. There is no need to create the setter method. We can modify our Student.java file something like this

File: Student.java

Java

 

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

 

public class Student {

 

    

    @Value("${student.rollNo}") private int rollNo;

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

    @Value("${student.age}") private int age;

 

    

    public void display()

    {

        

        System.out.println("Roll No: " + rollNo);

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

    }

}

All other things should be unchanged. Run your main() method again and the output will be like this.

Output:

Roll No: 101
Name: Sagar
Age: 20

Leave a Comment