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:
- Spring – Injecting Literal Values By Setter Injection
- Spring – How to Load Literal Values from Properties File
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
// Java Program to Illustrate Student Class // Class public class Student { // Class data members private int rollNo; private String name; private int age; // Setter public void setRollNo( int rollNo) { // this keyword refers to current instance itself this .rollNo = rollNo; } // Setter public void setName(String name) { this .name = name; } // Setter public void setAge( int age) { this .age = age; } // Method public void display() { // Printing attributes of corresponding student 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 <property> 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
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> < 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 > </ beans > |
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
// Java Program to Illustrate Application Class // Importing require classes import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; // Application class public class Main { // Main driver method public static void main(String[] args) { // Using ApplicationContext tom implement Spring IoC ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" ); // Getting the bean student Student student = context.getBean( "student" , Student. class ); // Calling display() method // inside main() method 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
// Java Program to Illustrate Student Class // Importing required classes import org.springframework.beans.factory.annotation.Value; // Class public class Student { // Class data members private int rollNo; private String name; private int age; @Value ( "101" ) // Setter public void setRollNo( int rollNo) { this .rollNo = rollNo; } @Value ( "Anshul" ) // Setter public void setName(String name) { this .name = name; } @Value ( "25" ) // Setter public void setAge( int age) { this .age = age; } // Method public void display() { // Print statement 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
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> < context:annotation-config /> < bean id = "student" class = "Student" > </ bean > </ beans > |
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
// Java Program to Illustrate Student Class // Importing required classes import org.springframework.beans.factory.annotation.Value; // Class public class Student { // Class data members private int rollNo; private String name; private int age; @Value ( "${student.rollNo}" ) // Setter public void setRollNo( int rollNo) { // this keyword refers to current instance itself this .rollNo = rollNo; } @Value ( "${student.name}" ) // Setter public void setName(String name) { this .name = name; } @Value ( "${student.age}" ) // Setter public void setAge( int age) { this .age = age; } // Method public void display() { // Printing attributes corresponding to student 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
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> < context:annotation-config /> < context:property-placeholder location = "classpath:student-info.properties" /> < bean id = "student" class = "Student" > </ bean > </ beans > |
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
// Java Program to Illustrate Student Class // Importing required classes import org.springframework.beans.factory.annotation.Value; // Class public class Student { // Class data members @Value ( "${student.rollNo}" ) private int rollNo; @Value ( "${student.name}" ) private String name; @Value ( "${student.age}" ) private int age; // Method public void display() { // Printing attributes corresponding to student 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