Dnyaneshwar Jadhav
Dnyaneshwar Jadhav

Reputation: 353

Springboot linking Environment values from Properties files but not from YAML files

This is strange, and now I am bit confused whats wrong in my approach.

I am using 2 Spring-Boot framework and writing simple Microservice apps.

Project-1

Constants.java

public static final String TEST_HOST                           = "${test1.host}"

application.properties

test1.host=https://somewhere.com

I am able to read this value without any problem i.e. https://somewhere.com.

Project-2

Constants.java

public static final String TEST_HOST                           = "${test1.host}"

application.yaml

test1:
  host: https://somewhere.com

In above case it is giving me the value as ${test1.host} I am expecting that expression should get execute, but its treating as constant with string value and not environment value.

Upvotes: 0

Views: 937

Answers (1)

Ali Arabat
Ali Arabat

Reputation: 118

Instead of doing this:

public static final String TEST_HOST = "${test1.host}"

Add the annotation @Value so that you can inject properties from the configuration file like the following snippet:

@Value("${test1.host}")
public String host;

The same thing for the second project.

Do not forget to decorate the class holding the config values with this annotation @Configuration.

The convenient appraoch to load config values

Upvotes: 1

Related Questions