I am trying to set below values in enviornment (Ubuntu 22.04)
spring.datasource.url = jdbc:mysql://localhost:3306/abc?useSSL=false
spring.datasource.username = root
spring.datasource.password = root
I have set values in enviornment as
export spring_datasource_url_prefix=mysql
export spring_datasource_host=localhost
export spring_datasource_port=3306
export spring_datasource_database=abc
export spring_datasource_username=root
export spring_datasource_password=root
And in my Spring Boot application.properties
file I have used it as
spring.datasource.url=jdbc:${spring_datasource_url_prefix}://${spring_datasource_host}:${spring_datasource_port}/${spring_datasource_database}
spring.datasource.username=${spring_datasource_username}
spring.datasource.password=${spring_datasource_password}
But I am unable to fetch values using this. Am I missing anything or doing it wrong? I have tried setting enviornment through nano .bashrc
and as mentioned above.
I am getting error as
Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:538) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
I have also tried it as below
spring.datasource.url=jdbc:${spring_datasource_url_prefix}://${spring_datasource_host}:${spring_datasource_port}/${spring_datasource_database}
spring.datasource.username=${spring_datasource_username}
spring.datasource.password=${spring_datasource_password}
I am getting error as below
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
[2m2023-05-15 17:30:06.909[0;39m [32m INFO[0;39m [35m2208925[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Stopping service [Tomcat]
[2m2023-05-15 17:30:06.929[0;39m [32m INFO[0;39m [35m2208925[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mConditionEvaluationReportLoggingListener[0;39m [2m:[0;39m
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2m2023-05-15 17:30:06.931[0;39m [31mERROR[0;39m [35m2208925[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m:[0;39m
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
2
Answers
Don’t. You are really overcomplicating things. Leave the application context as is, don’t change. Name the environment variables
SPRING_DATASOURCE_URL
(all uppercase) and Spring Boot will automatically use those instead of the values from theapplication.properties
.Spring Boot will automatically use these, instead of the ones from
application.properties
.You can refer below website’s detailed explanation:
Baledung – env variables
JavaRevisited – env variables
Other things to consider are: