Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can spring boot get spring datasource config value from os environment variable?

this is my spring datasource config

spring:
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver
    url: ex.com
    username: exId
    password: exPw

and we know we can use os environment variable for spring datasource config like this

# export SPRING_DARASOURCE_URL=ex.com

But we can not export SPRING_DARASOURCE_DRIVER-CLASS-NAME because of ‘-‘ like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# export SPRING_DARASOURCE_DRIVER-CLASS-NAME=org.mariadb.jdbc.Driver

So if i wanna get spring.datasource.driver-class-name via os environment variable,
what i have to do?

>Solution :

@JIN

In most cases, any punctuation like those hyphens can be converted to underscores for the system environment variable, e.g. SPRING_DATASOURCE_DRIVER_CLASS_NAME. In some earlier versions of Spring this wasn’t exactly standardized yet and some properties might drop the hyphens entirely, e.g. SPRING_DATASOURCE_DRIVERCLASSNAME.

Another approach: set the system env var, JAVA_TOOL_OPTIONS, with any desired Java/JVM options like:
JAVA_TOOL_OPTIONS=-server -Xmx1g -Dspring.datasource.driver-class-name=com.mysql.jdbc.Driver

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading