Spring @Value Annotation

Published on August 3, 2022
Default avatar

By Pankaj

Spring @Value Annotation

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Spring @Value annotation 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. Spring @Value annotation also supports SpEL. Let’s look at some of the examples of using @Value annotation.

Spring @Value - Default Value

We can assign default value to a class property using @Value annotation.

@Value("Default DBConfiguration")
private String defaultName;

@Value annotation argument can be a string only, but spring tries to convert it to the specified type. Below code will work fine and assign the boolean and integer values to the variable.

private boolean defaultBoolean;

private int defaultInt;

Spring @Value - Spring Environment Property

private String defaultAppName;

If the key is not found in the spring environment properties, then the property value will be ${APP_NAME_NOT_FOUND}. We can assign a default value that will get assigned if the key is missing from spring environment properties.

private String defaultAppName;

Spring @Value - System Environment

When Spring environment is populated, it reads all the system environment variables and stores it as properties. So we can assign system variables too using @Value annotation.

private String javaHome;
private String homeDir;

Spring @Value - SpEL

We can also use Spring Expression Language with @Value annotation. So we can read java home system property using SpEL too.

private String javaHome;

Spring @Value with methods

When the @Value annotation is found on a method, Spring context will invoke it when all the spring configurations and beans are getting loaded. If the method has multiple arguments, then every argument value is mapped from the method annotation. If we want different values for different arguments then we can use @Value annotation directly with the argument.

public void printValues(String s, String v){} //both 's' and 'v' values will be 'Test' 
public void printValues(String s, @Value("Data") String v){}
// s=Test, v=Data

Spring @Value Example

Let’s create a simple Spring application where we will use @Value annotation to read properties and assign them to class variables. Create a maven project and add spring core dependencies.


Our final project structure will look like below image, we will look each of the components one by one. Spring Value Annotation Example Create a component class where we will inject variable values through @Value annotation.

package com.journaldev.spring;

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

public class DBConnection {

	private String driverClass;
	private String dbURL;
	private String userName;
	private char[] password;

	public DBConnection() {

	public void printDBConfigs() {
		System.out.println("Driver Class = " + driverClass);
		System.out.println("DB URL = " + dbURL);
		System.out.println("User Name = " + userName);

		// Never do below in production environment :D
		System.out.println("Password = " + String.valueOf(password));

Now we have to create a Configuration class and provide a @Bean method for DBConnection class.

package com.journaldev.spring;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@PropertySource(value = "classpath:root.properties", ignoreResourceNotFound = true)
public class DBConfiguration {

	@Value("Default DBConfiguration")
	private String defaultName;

	private boolean defaultBoolean;

	private int defaultInt;

	private String defaultAppName;

	// @Value("#{systemProperties['java.home']}")
	private String javaHome;

	private String homeDir;

	public DBConnection getDBConnection() {
		DBConnection dbConnection = new DBConnection();
		return dbConnection;

	public void printValues(String s, @Value("another variable") String v) {
		System.out.println("Input Argument 1 =" + s);
		System.out.println("Input Argument 2 =" + v);

		System.out.println("Home Directory = " + homeDir);
		System.out.println("Default Configuration Name = " + defaultName);
		System.out.println("Default App Name = " + defaultAppName);
		System.out.println("Java Home = " + javaHome);
		System.out.println("Boolean = " + defaultBoolean);
		System.out.println("Int = " + defaultInt);



Here is our main class where we are creating annotation-based spring context.

package com.journaldev.spring;

import java.sql.SQLException;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringMainClass {

	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		System.out.println("Refreshing the spring context");
		DBConnection dbConnection = context.getBean(DBConnection.class);

		// close the spring context


When you will run the class, it will produce following output.

Input Argument 1 =Test
Input Argument 2 =another variable
Home Directory = /Users/pankaj
Default Configuration Name = Default DBConfiguration
Default App Name = Default
Java Home = /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
Boolean = true
Int = 10
Refreshing the spring context
Driver Class = com.mysql.jdbc.Driver
DB URL = jdbc:mysql://localhost:3306/Test
User Name = journaldev
Password = journaldev

Notice that Configuration class printValues() is getting called before our context is ready to serve user requests. That’s all for Spring @Value annotation example, you can download the example code from our GitHub Repository.

Spring @Value Annotation Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors
Default avatar


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
DigitalOcean Employee
DigitalOcean Employee badge
August 12, 2020

hi When i try to use @value iam getting null value application.properties: spring.datasource.username:root HibernateUtil.java: @Value(“${spring.datasource.username}”) public String user; on printing user,it gives null

- Prakashkumar P

    DigitalOcean Employee
    DigitalOcean Employee badge
    September 19, 2018

    Wow, this is very useful! Thank you.

    - Nelda

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel