Spring Top – Temp

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

Lightrun – Third Party Code
announcement - icon

Flakiness in REST requests is a common issue. A request can get a 200 OK in one scenario and a 409 next time. Sometimes a request can even succeed and fail intermittently on the same exact request. In short, working over HTTP can be a bit of a mess without solid tooling.

Also, while it’s easy enough to debug these issues locally when developing the application, we’re talking about production here - we can’t afford the downtime while you’re stepping in and out of code. Uptime is kind of the whole point.

With Lightrun, you can get the same level of access you get with a local debugger or profiler - no downtime required. You can add logs, metrics, and snapshots (think breakpoints, but without stopping the running service), in a safe and read-only manner - without redeploying, restarting, or even stopping the running service. Performance and security are maintained throughout the process.

Learn how to debug a live REST API (built with Spring, of course), using Lightrun, in this 5-minute tutorial:

>> Debugging REST Requests in Spring-Based applications using the Lightrun Platform

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 tutorial — 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, stringWithDefaultValue will be set to the default value of my default value.

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

private String stringWithBlankDefaultValue;

3. Primitives

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

private boolean booleanWithDefaultValue;
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:

private String[] stringArrayWithDefaults;

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

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!