Main Tutorials

Spring @Value default value

This article shows how to provide a default value for the @Value annotation.

In the below code, if the property.name doesn’t exist in the application properties or environment, defaultValue will be assigned to the propertyName field.

SimpleComponent.java

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

@Component
public class SimpleComponent {

  @Value("${property.name:defaultValue}")
  private String propertyName;

  // getters, setters, etc
}

Table of contents:

P.S. Tested with Spring Boot 3.1.2

1. Default value for @Value

Review the project structure.

default value for @Value

1.1 Spring default loads the application.properties from the classpath root, /src/java/resources.

application.properties

  db.name=mkyong

1.2 In the below code, we use @Value to access the value defined with the db.name property key. If the db.name property key doesn’t exist in the application properties or environment, the hello (default value) will be assigned to the name field.

DatabaseService.java

package com.mkyong;

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

@Component
public class DatabaseService {

  @Value("${db.name:hello}")
  private String name;  // if db.name doesn't exist, we get hello

  public String getName() {
      return name;
  }

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

Starts the Spring Boot application.

SpringBootApplication.java

package com.mkyong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;

@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication implements CommandLineRunner {

  @Autowired
  private DatabaseService dbConfig;

  @Override
  public void run(String... args) throws Exception {
      System.out.println(dbConfig.getName());
  }

  public static void main(String[] args) {
      SpringApplication.run(SpringBootApplication.class, args);
  }
}

Output

Terminal

  mkyong

1.3 Now, we comment out the property key db.name in the application.properties.

application.properties

# comment out to test the default value
# db.name=mkyong

Rerun the application, and we get the output hello.

Terminal

  hello

2. Testing the default value for @Value

2.1 We create a new test.properties with empty content for testing.

test/resources/test.properties

# comment to test the default value
# db.name=mkyong

2.2 We can use @TestPropertySource to load values from the test.properties file.

ApplicationTest.java

package com.mkyong;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertEquals;

// easy but load entire context, no good.
// @SpringBootTest

// Keep test in minimal configuration
@SpringJUnitConfig
@TestPropertySource("classpath:test.properties")
public class ApplicationTest {

  @Autowired
  private DatabaseService dbServer;

  @Test
  public void testDefaultDatabaseName() {
      assertEquals("hello", dbServer.getName());
  }

  // Mock Configuration within the test class
  @Configuration
  static class TestConfig {
      @Bean
      public DatabaseService dbServer() {
          return new DatabaseService();
      }
  }

}

3. Download Source Code

$ git clone https://github.com/mkyong/spring-boot.git

$ cd spring-boot-externalize-config-4

$ mvn test

$ mvn spring-boot:run

4. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Shreyas Nargund
6 years ago

can you assign the default value with another property?
I tries something like this and it threw an error.

@Value(“${properties.myProperty:${properties.defaultProperty}}”)
private String myProperty;

Vincent
8 years ago

Does ${property:default value} work as expected in Spring 3.2? I get “${property:default value}” but not “default value” when “property” not exist.

Eugene
2 years ago

not working for fresh spring. spring boot 2.3.4

Ciccio
5 years ago

thank you minkiong

Vigneshwaran Ganesan
8 years ago

Hi mkyong,

I have tried to implement the Annotation for reading the
property file but getting null for environment.

@Configuration
@PropertySource(“classpath: application.properties”)
Class Level

Inside the class
@Autowired
private Environment env;

Inside the Method
env.getProperty(key)

Conclusion: I am getting null value for env.getProperty(key).

Daniel Garay
8 years ago

Hi!
Whats youre plugin js or css for the language formatter?
Its very pretty