Spring Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

Spring's @Value annotation provides a convenient way to inject property values into components. It's also quite useful to provide sensible defaults for cases where a property may not be present.

That's what we're going to be focusing on in this article – how to specify a default value for the @Value Spring annotation. For a more detailed quick guide on @Value, see the article here.

Further reading:

A Quick Guide to Spring @Value

Learn to use the Spring @Value annotation to configure fields from property files, system properties, etc.

Properties with Spring and Spring Boot

Tutorial for how to work with properties files and property values in Spring.

Spring Expression Language Guide

This article explores Spring Expression Language (SpEL), a powerful expression language that supports querying and manipulating object graphs at runtime.

2. String Defaults

Let's look at the basic syntax for setting a default value for a String property:

@Value("${some.key:my default value}")
private String stringWithDefaultValue;

If some.key cannot be resolved, then stringWithDefaultValue will be set to the default value of “my default value”.

Similarly, we can set a zero-length String as the default value:

@Value("${some.key:})"
private String stringWithBlankDefaultValue;

3. Primitives

To set a default value for primitive types such as boolean and int, we use the literal value:

@Value("${some.key:true}")
private boolean booleanWithDefaultValue;
@Value("${some.key:42}")
private int intWithDefaultValue;

If we wanted to, we could use primitive wrappers instead by changing the types to Boolean and Integer.

4. Arrays

We can also inject a comma-separated list of values into an array:

@Value("${some.key:one,two,three}")
private String[] stringArrayWithDefaults;

@Value("${some.key:1,2,3}")
private int[] intArrayWithDefaults;

In the first example above, the values “one”, “two”, and “three” are injected as defaults into stringArrayWithDefaults.

In the second example, the values 1, 2, and 3 are injected as defaults into intArrayWithDefaults.

5. Using SpEL

We can also use Spring Expression Language (SpEL) to specify an expression and a default.

In the example below, we expect some.system.key to be set as a system property, and if it is not set, we want to use “my default system property value” as a default:

@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;

6. Conclusion

In this quick article, we looked at how we can set a default value for a property whose value we would like to have injected using Spring's @Value annotation.

As usual, all the code samples used in this article can found in the GitHub project.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Juan M
2 years ago

Nice article, btw, I found a typo in the second block of code:

@Value(“${some.key:}”
private String stringWithBlankDefaultValue;

At the end of @Value expression, there are a missing a bracket

Grzegorz Piwowarek
2 years ago
Reply to  Juan M

Thanks, we will fix that soon 🙂

Comments are closed on this article!