0

I am using to configure spring boot with an external YAML configuration and CMD.

-> application.yml file

spring:
  profiles: integration-test
  datasource:
    driverClassName: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
    url: ${SPRING_DATASOURCE_URL}
    username: ${SPRING_DATASOURCE_USERNAME}
    password: ${SPRING_DATASOURCE_PASSWORD}

-> cmd

mvn clean install

-> Result

Caused by: java.lang.IllegalStateException: Cannot load driver class: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}

Can anyone explain this to me?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Monoem Youneb
  • 95
  • 1
  • 12

2 Answers2

0

Pass those variables in your launch configuration of your program or at commandline when you run your app with java YourMainClass, e.g.

java -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=<full_qualified_name_of_your_jdbc_driver_class> -DSPRING_DATASOURCE_URL=<jdbc_url> YourMainClass

also pass the other two variables the same way, username & password!

your can even set those enviroment variables on OS level, so you don't have to set them each time you start your application...

if your using Spring Boot also have a look at this one: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

0

When you use the syntax ${}, you are actually telling Spring Boot to use the value of the property whose name is between brackets. In your case, Spring Boot tries to resolve the property SPRING_DATASOURCE_DRIVER_CLASS_NAME. When it fails, it uses the string as is, which leads to the error you mentioned, since no driver exists under the name ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}.

To solve the issue, you can either :

  1. replace the ${} by the real values, e.g. driverClassName: org.postgresql.Driver and do the same for the other properties (url, username and password)
  2. provide properties SPRING_DATASOURCE_DRIVER_CLASS_NAME,SPRING_DATASOURCE_URL and the two others. These can passed in the command line with -D options (e.g. -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver) or through environment variables. You can look at spring Boot documentation for more details.
HL'REB
  • 838
  • 11
  • 17