Using Spring's @value with Defaults

Some values of an application have to be provided from “the outside” of the application, for example the secret of a JSON Web Token (JWT). This secret must remain private and must not be shared. Hence, it cannot be written in a properties file, especially not in an open-sourced project.

In a local environment and also in a deployed state, this can be done by providing an environment variable:


This can be extended to provide a default value if no environment variable has been set. That makes running unit test on a CI server much easier because this server does not have to be configured:

@Value("${jwt.secret ?: 'A long-enough string, for example this one because its really long and so secure. Yeay! This is a really long string.'}")
private String jwtSecret;