How to specify external YAML configuration file for springboot

Viewed 5132

I'm trying to pass in a value to be autowired from a custom configuration file in Spring boot. Below are code snippets:

Spring class

@Configuration
public class MyConfig {

    @Value("${BOOTSTRAP_SERVERS}")
    private String bootstrapServers;

myfile.yaml

BOOTSTRAP_SERVERS: 
  10.0.0.12:9092

Execution command

java  -jar app.jar --spring.config.location=/file/path/myfile.yaml

However when I type the above command I'm getting this error:

java.lang.IllegalArgumentException: Could not resolve placeholder 'BOOTSTRAP_SERVERS' in value "${BOOTSTRAP_SERVERS}"

What am I missing here in order to get this to work? I intend to mount the app in kubernetes so I need to be able to externalize my configuration. Thanks in advance.

1 Answers

Apparently it is due to invalid path declaration, to configure external properties/yml files you must use file: prefix for --spring.config.location.

So try this,

--spring.config.location="file:/path/to/myfile.yaml"

An alternative would be,

-Dspring.config.location="file:/path/to/myfile.yaml"

Make sure myfile.yaml is in the directory.


Official documentation: https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Related