java 在Ubuntu 22.04中为Sping Boot 应用程序设置环境中的值

ztmd8pv5  于 2023-05-15  发布在  Java
关注(0)|答案(2)|浏览(101)

我试图在环境中设置以下值(Ubuntu 22.04)

spring.datasource.url = jdbc:mysql://localhost:3306/abc?useSSL=false
spring.datasource.username = root
spring.datasource.password = root

我在环境方面的价值观是

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

在我的Sping Boot application.properties文件中,我使用它作为

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}

但我无法使用此方法获取值。我是不是错过了什么,还是做错了?我已经尝试过通过nano .bashrc和上面提到的设置环境。
我得到的错误是

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]

我也试过如下

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}

我得到的错误如下

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
qij5mzcb

qij5mzcb1#

别。你把事情弄得太复杂了。保持应用程序上下文不变,不要更改。将环境变量命名为SPRING_DATASOURCE_URL(全部大写),Sping Boot 将自动使用这些变量,而不是application.properties中的值。

export SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/abc?useSSL=false 
export SPRING_DATASOURCE_USERNAME=root 
export SPRING_DATASOURCE_PASSWORD=root

Sping Boot 将自动使用这些,而不是来自application.properties的。

uxhixvfz

uxhixvfz2#

您可以参考以下网站的详细说明:
Baledung - env variables
JavaRevisited - env变量
其他需要考虑的事项包括:
1.执行.bashrc后,是否验证了导出的变量已成功注册并可用于应用程序。
1.哪个配置文件像dev,uat或这些直接设置在“application.properties“?

相关问题