You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04下Spring Boot数据源环境变量配置失败求助

问题

我在Ubuntu 22.04中尝试配置Spring Boot数据源环境变量,目标配置如下:

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

并在Spring 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}

但应用无法获取这些变量的值,我试过编辑.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]

重新尝试后出现的新错误:

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

请问我哪里操作有误或遗漏了什么?


分析与解决方法

  • 环境变量未正确加载
    编辑.bashrc后,需要执行source ~/.bashrc重新加载配置,或者重启终端。如果是通过IDE启动应用,需确保IDE能读取到系统环境变量——部分IDE需要重启才能加载新变量,也可以在IDE的运行配置中手动指定这些环境变量。

  • Spring Boot环境变量命名规范问题
    Linux环境变量常规为全大写格式,Spring Boot会自动将大写+下划线的变量映射为小写+点的配置项。建议修改环境变量为全大写:

    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
    

    application.properties中的引用可以保持不变,Spring Boot能自动识别映射。

  • 缺少MySQL驱动依赖
    第二个错误提示无法找到合适的驱动类,说明项目未引入MySQL JDBC驱动。需在项目依赖中添加:
    Maven项目(pom.xml):

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    

    Gradle项目(build.gradle):

    runtimeOnly 'mysql:mysql-connector-java'
    
  • URL拼接缺失必要参数
    目标URL包含?useSSL=false,当前拼接的URL未包含该参数,需在application.properties中补充:

    spring.datasource.url=jdbc:${spring_datasource_url_prefix}://${spring_datasource_host}:${spring_datasource_port}/${spring_datasource_database}?useSSL=false
    
  • 验证环境变量是否生效
    在终端执行echo $SPRING_DATASOURCE_URL_PREFIX(修改为全大写后),确认变量值是否正确输出。若输出为空,需检查.bashrc的编辑是否有误,比如拼写错误、是否编辑了正确用户目录下的.bashrc文件。

内容的提问来源于stack exchange,提问作者G Shares

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:22:49