×
☰ See All Chapters

Spring @Value Annotation

@Value annotation can be used for injecting default values. This can be used at field level, parameters level of constructor and method. However the class where we used @Value annotation should be spring managed bean.  We can inject values from properties files, in our next chapter Spring @PropertySource Annotation we learn to inject values from properties files. Also we can inject values through SpEL (Spring Expression Language), means @Value annotation supports SpEL to compute the values.

Let us see an example for how we can use @Value annotation to inject default value.   Followed by this example we have given another example which shows how we can use SpEL to inject values.

Spring @Value Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Value</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Message.java

package com.java4coding;

 

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

 

public class Message {

       

        public String message;

       

        @Value("Manu Manjunatha")

        public String authorName;

       

}

 

Employee.java

package com.java4coding;

 

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

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

import org.springframework.stereotype.Component;

 

@Component

public class Employee {

 

        public String employeeName;

       

        //constructor should be annotated with @Autowired, otherwise we get BeanCreationException as we don't have default constructor

        @Autowired

        public Employee(@Value("Advith")String employeeName) {

                this.employeeName = employeeName;

        }

}

SpringConfiguration.java

package com.java4coding;

 

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

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

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

@Configuration

public class SpringConfiguration {

       

        @Autowired

        Employee  employee;

       

        @Bean

        public Message getMessageBean(@Value("Welcome to www.java4coding.com")String message) {

                Message messageObj = new Message();

                messageObj.message = message;

                return messageObj;

        }

       

        @Bean

        public Employee getEmployeeBean () {

                return employee;

        }

}

Demo.java

package com.java4coding;

 

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

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

        @Autowired

        Employee  employee;

       

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.java4coding");

 

                Message message = (Message) ctx.getBean("getMessageBean");

                System.out.println(message.authorName);

                System.out.println(message.message);

               

                Employee employee = (Employee)ctx.getBean("getEmployeeBean");

                System.out.println(employee.employeeName);

        }

       

}

Output

spring-value-annotation-0
 

Spring @Value Annotation SpEL Example

@Value annotation also supports SpEL, below is an example on how to use SpEL with @Value annotation.

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Value</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Subject.java

package com.java4coding;

 

public class Subject {

        private String name;

        private int id;

 

        public Subject() {

        }

       

        public Subject(String name, int id ) {

                this.name =  name;

                this.id = id;

        }

       

        public String getName() {

                return name;

        }

 

        public void setName(String name) {

                this.name = name;

        }

 

        public int getId() {

                return id;

        }

 

        public void setId(int id) {

                this.id = id;

        }

}

Student.java

package com.java4coding;

 

import java.util.List;

 

public class Student {

        private String name;

        private List<Subject> subjects;

        private List<Subject> topSubjects;

        private List<String> toughSubjects;

 

        public String getName() {

                return name;

        }

 

        public void setName(String name) {

                this.name = name;

        }

 

        public List<Subject> getSubjects() {

                return subjects;

        }

 

        public void setSubjects(List<Subject> subjects) {

                this.subjects = subjects;

        }

 

        public List<Subject> getTopSubjects() {

                return topSubjects;

        }

 

        public void setTopSubjects(List<Subject> topSubjects) {

                this.topSubjects = topSubjects;

        }

 

        public List<String> getToughSubjects() {

                return toughSubjects;

        }

 

        public void setToughSubjects(List<String> toughSubjects) {

                this.toughSubjects = toughSubjects;

        }

}

SpringConfiguration.java

package com.java4coding;

 

import java.util.ArrayList;

import java.util.List;

 

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

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

@Configuration

public class SpringConfiguration {

       

        @Bean

        public List<Subject> subjectList() {

                List<Subject> list = new ArrayList<Subject>();

                list.add(new Subject("JAVA", 110));

                list.add(new Subject("Servlet", 111));

                list.add(new Subject("JSP", 112));

                list.add(new Subject("EJB", 113));

                return list;

        }

       

        @Value("#{'Manu M'}")

        private String name;

       

        @Value("#{subjectList}")

        private List<Subject> subjects;

       

        @Value("#{subjectList.?[id gt 111 and id lt 113]}")

        private List<Subject> topSubjects;

       

        @Value("#{subjectList.?[id le 111].![id+':'+name]}")

        private List<String> toughSubjects;

       

        @Bean

        public Student studentBean ( ) {

                Student student = new Student();

                student.setName(name);

                student.setSubjects(subjects);

                student.setTopSubjects(topSubjects);

                student.setToughSubjects(toughSubjects);

                return student;

        }

}

Demo.java

package com.java4coding;

 

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

 

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("com.java4coding");

 

                Student student = (Student)ctx.getBean("studentBean");

               

                System.out.println("Student Name: " + student.getName());

               

                System.out.println("-------------All Subjects--------------");

                student.getSubjects().stream().forEach(sub -> System.out.println("Id: " + sub.getId() + " Name: " + sub.getName()));

               

                System.out.println("-------------Top Subjects--------------");

                student.getTopSubjects().stream().forEach(sub -> System.out.println("Id: " + sub.getId() + " Name: " + sub.getName()));

 

                System.out.println("-------------Tough Subjects--------------");

                System.out.println(student.getToughSubjects());

        }

}

Output

spring-value-annotation-1
 

All Chapters
Author