Does application.yml support environment variables?
JavaSpringYamlSpring BootJava 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;