'Does application.yml support environment variables?
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
Solution 1:[1]
Try ${OPENSHIFT_DIY_PORT}
(the usual Spring placeholder notation). See here for docs.
Solution 2:[2]
You even can add default value, if environment variable not provided:
logging:
level:
root: ${LOGGING_LEVEL_ROOT:info}
Solution 3:[3]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andy Wilkinson |
Solution 2 | Oleksandr Yefymov |
Solution 3 |