I have some code to load a value as such in my Spring application:
@Component
public class MyElasticRestService {
@Value("${elasticApi.baseURL}")
private String elasticApiBaseUrl;
According to the Spring docs, I should be able to use a relaxed binding that comes from an uppercase environment variable such as ELASTIC_API_BASE_URL
or ELASTICAPI_BASEURL
. But I'm confused which is correct. Both don't seem to work so I am wondering how to debug what is actually picked up.
I've loaded Spring Boot Actuator to view the configprops
endpoint. But it doesn't have anything on the elasticApi
prefix.
What should the correct environment variable be and how can I see how it gets translated and picked up by the application?
The @Value
annotation doesn't support relaxed bindings. Therefore you could use a class annotated with @ConfigurationProperties
or you use a RelaxedPropertyResolver
to get the value from the environment.
According to https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-vs-value, it is now very possible simply with @Value
as long as you use kebab-case (all lower case with dash) for the name e.g. @Value("config.refresh-rate")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With