设置Ubuntu 22.04上的Spring Boot应用程序环境中的数值。

huangapple go评论56阅读模式
英文:

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

以下是翻译好的部分:

您可以参考以下网站的详细解释:

其他需要考虑的事项包括:

  1. 在执行您的 .bashrc 后,您是否验证了导出的变量是否成功注册并对应于应用程序。
  2. 像 dev、uat 或这些是直接设置在 "application.properties" 中的哪个配置文件?
英文:

You can refer below website's detailed explanation:
Baledung - env variables
JavaRevisited - env variables

Other things to consider are:

  1. After executing your .bashrc, did you verify the exported variables are registered successfully and available to the application.
  2. Which profile like dev,uat or these directly set in the "application.properties"?

huangapple
  • 本文由 发表于 2023年5月15日 14:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定