5

As described in the Spring Boot documentation, configuration can be passed via environment variables. However there are some configuration properties like spring.datasource.driver-class-name which contain hyphens. When setting this in bash you will get an error:

$ export SPRING_DATASOURCE_DRIVER-CLASS-NAME=com.mysql.jdbc.Driver
bash: export: `SPRING_DATASOURCE_DRIVER-CLASS-NAME=com.mysql.jdbc.Driver': not a valid identifier

This is because variables in bash must not contain hyphens. So is it even possible to set Spring configuration properties which contain hyphens via environment variables?

britter
  • 1,352
  • 1
  • 11
  • 26
  • Check if this helps http://unix.stackexchange.com/questions/23659/can-shell-variable-include-character or you can consider passing them as java system properties instead i.e., `-Dspring.datasource.driver-class-name=....` – Madhusudana Reddy Sunnapu Mar 29 '16 at 14:06
  • @MadhusudanaReddySunnapu that would certainly do the trick, thank you! But I want to pass the configuration as environment variable, so it doesn't really help in my case ;-) – britter Mar 29 '16 at 14:08
  • 3
    Try replacing the hyphens with underscores. The relaxed binding should do the work – joshiste Mar 29 '16 at 14:10
  • 4
    What version are you using? Try `SPRING_DATASOURCE_DRIVER_CLASS_NAME` – Stephane Nicoll Mar 29 '16 at 14:24
  • @joshiste that did the trick, thank you! – britter Mar 30 '16 at 08:09
  • @StéphaneNicoll it works. However it is a bit confusing. When setting `SPRING_DATASOURCE_DRIVER_CLASS_NAME` only `spring.datasource.driver.class.name` is set while `spring.datasource.driver-class-name` will be null. All of the docs just talk about the latter, that's why I was going with the hyphens. – britter Mar 30 '16 at 08:11
  • Sorry I didn't get that. Does my solution work? (it should). Feel free to open an issue/PR with more details please. – Stephane Nicoll Mar 30 '16 at 08:26
  • @StéphaneNicoll yes it does work. Thank you! – britter Mar 30 '16 at 08:44
  • @joshiste do you want to post the answer? – britter Mar 31 '16 at 09:00

1 Answers1

13

Spring Boot provides a relaxed binding. For this reason the environment variable SPRING_DATASOURCE_DRIVER_CLASS_NAME can be used to set the driver class name.

britter
  • 1,352
  • 1
  • 11
  • 26
  • 2
    `DRIVER_CLASS_NAME`? The table in the link you provided suggests it is `SPRING_DATASOURCE_DRIVERCLASSNAME` (`.`→`_`, but `-` in name are just dropped, not also converted to `_`s). – Jan Hudec Apr 27 '20 at 12:33
  • @JanHudec I found this link that may answer your question. https://stackoverflow.com/a/40612891/4230091 – Amar Deep Singh Dec 05 '22 at 13:12