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

Time:11-18

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

# 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?

CodePudding user response:

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
  • Related