英文:
Set values in environment for spring boot application in Ubuntu 22.04
问题
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
<details>
<summary>英文:</summary>
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
</details>
# 答案1
**得分**: 0
不要。你确实把事情弄得太复杂了。保留应用程序上下文不变。将环境变量命名为`SPRING_DATASOURCE_URL`(全部大写),Spring Boot将自动使用这些值,而不是`application.properties`中的值。
```bash
export SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/abc?useSSL=false
export SPRING_DATASOURCE_USERNAME=root
export SPRING_DATASOURCE_PASSWORD=root
Spring Boot将自动使用这些值,而不是application.properties
中的值。
英文:
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 the application.properties
.
export SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/abc?useSSL=false
export SPRING_DATASOURCE_USERNAME=root
export SPRING_DATASOURCE_PASSWORD=root
Spring Boot will automatically use these, instead of the ones from application.properties
.
答案2
得分: 0
以下是翻译好的部分:
您可以参考以下网站的详细解释:
其他需要考虑的事项包括:
- 在执行您的 .bashrc 后,您是否验证了导出的变量是否成功注册并对应于应用程序。
- 像 dev、uat 或这些是直接设置在 "application.properties" 中的哪个配置文件?
英文:
You can refer below website's detailed explanation:
Baledung - env variables
JavaRevisited - env variables
Other things to consider are:
- After executing your .bashrc, did you verify the exported variables are registered successfully and available to the application.
- Which profile like dev,uat or these directly set in the "application.properties"?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论