How to use environment variable in custom yaml file in spring boot application?

Viewed 303

I have an external custom yaml file in classpath named cache-config.yaml and I want to use environment variables in that file as;

cache-name-1:
  ttl: ${redis.global-ttl:144000}
  maxIdleTime: ${redis.global-max-idle-time:60000}

When I try to read this yaml it shows variables as String not reading the properties from environment. Can you please show a way that I can use envs in this yaml. Thank you

1 Answers

If you want to use an external properties file you need to tell your program where to read it from by adding it as a spring option when running your application. Something like this should work:

java -jar myJar.jar --spring.config.additional-location=file:/path/to/file/cache-config.yaml

Related