Does application.yml support environment variables?

JavaSpringYamlSpring Boot

Java Problem Overview


I tried using env variables in my application.yml configration like:

spring:
  main:
    show_banner: false

---

spring:
  profiles: production
server:
  address: $OPENSHIFT_DIY_IP
  port: $OPENSHIFT_DIY_PORT

but the env variables are not resolved. Do I have to provide a different notation?

In Rails you can e.g. use <%= ENV['FOOVAR'] %>

The only alternative is to run the app like:

java -jar my.jar --server.address=$OPENSHIFT_DIY_IP --server.port=$OPENSHIFT_DIY_PORT

Java Solutions


Solution 1 - Java

Try ${OPENSHIFT_DIY_PORT} (the usual Spring placeholder notation). See here for docs.

Solution 2 - Java

You even can add default value, if environment variable not provided:

logging:
  level:
    root: ${LOGGING_LEVEL_ROOT:info}

Solution 3 - Java

In summary YES. You can use the @Value to load the environment variables from the application.yml or application.properties

Additionally, you can load variables and cast them automatically if you need different data types to perform your validations and business logic.

server:
  address: ${OPENSHIFT_DIY_IP}
  port: ${OPENSHIFT_DIY_PORT}

Load information

@Value("${server.address}")
private String serverAddress;

@Value("${server.port}")
private Integer serverPort;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcel OverdijkView Question on Stackoverflow
Solution 1 - JavaDave SyerView Answer on Stackoverflow
Solution 2 - JavaOleksandr YefymovView Answer on Stackoverflow
Solution 3 - JavaJorge TovarView Answer on Stackoverflow