Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Spring camel case property with uppercase environment variable?

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?

like image 289
Andy Shinn Avatar asked Feb 20 '16 01:02

Andy Shinn


2 Answers

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.

like image 100
joshiste Avatar answered Sep 17 '22 15:09

joshiste


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")

like image 36
sancho21 Avatar answered Sep 16 '22 15:09

sancho21