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

Spring Boot客户端DataSource配置报错:未指定url及驱动类问题

DataSource配置错误排查:'url'属性未指定且无法配置嵌入式数据源

错误信息

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class.

环境配置

pom.xml依赖

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
      <version>4.1.0</version>
   </dependency>
   <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <scope>42.6.1</scope>
   </dependency>
   <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>5.7.0</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-rest-webmvc</artifactId>
   </dependency>
   <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct</artifactId>
      <version>1.6.0.Beta1</version>
   </dependency>
   <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-processor</artifactId>
      <version>1.6.0.Beta1</version>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-dependencies</artifactId>
      <version>4.1.0</version>
      <type>pom</type>
   </dependency>
   <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>1.1.0.Final</version>
   </dependency>
   <dependency>
      <groupId>jakarta.validation</groupId>
      <artifactId>jakarta.validation-api</artifactId>
      <version>3.0.2</version>
   </dependency>
   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
      <version>2.3.0</version>
   </dependency>
   <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.1.1</version>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
   </dependency>
    <dependency>
        <groupId>com.vaadin.external.google</groupId>
        <artifactId>android-json</artifactId>
        <version>0.0.20131108.vaadin1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <excludes>
               <exclude>
                  <groupId>org.projectlombok</groupId>
                  <artifactId>lombok</artifactId>
               </exclude>
            </excludes>
         </configuration>
      </plugin>
   </plugins>
</build>

客户端配置

启动类

@Configuration
@SpringBootApplication
public class AdminApiApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApiApplication.class, args);
}
}

bootstrap.yml

spring:
cloud:
config:
uri: http://localhost:8888
enabled: true
name: spring-cloud-config-server

management:
endpoints:
web:
exposure:
include: "*"

配置服务器配置

application.yml

server:
port: 8888

spring:
profiles:
active: git
security:
user:
name: ****
password: ****

logging:
level:
org.springframework.cloud.config: DEBUG

spring:
cloud:
config:
server:
git:
uri: https://github.com/*****/****.git
username: *****
password: *****
cloneOnStart: true

Git仓库中的配置

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres?currentSchema=postgres
    username: postgres
    password: postgres
    driverClassName: org.postgresql.Driver
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
      dialect: org.hibernate.dialect.PostgreSQLDialect
server:
  port: 8080

问题说明

客户端能成功从配置服务器读取属性,但启动时仍触发上述DataSource配置错误,需解决该问题以完成正确的DataSource配置。


解决方案

1. 修复PostgreSQL依赖的配置错误

pom.xml中PostgreSQL依赖的scope被错误设置为版本号42.6.1,正确写法是把版本号放在version标签,scope设为runtime(Spring Boot推荐,确保驱动在运行时可用):

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.1</version>
    <scope>runtime</scope>
</dependency>

原错误会导致驱动类无法被加载,直接触发找不到驱动的报错。

2. 适配Spring Cloud 4.x的配置加载逻辑

Spring Boot 3.x搭配Spring Cloud 4.x时,默认不再启用bootstrap.yml的自动加载,需要添加以下依赖开启该功能:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
    <version>4.1.0</version>
</dependency>

或者替换bootstrap.yml的配置方式,在客户端application.yml中添加:

spring:
  config:
    import: configserver:http://localhost:8888

确保配置服务器的数据源属性在DataSource初始化前完成加载。

3. 移除不必要的依赖

客户端不需要引入spring-cloud-config-server依赖(这是配置服务器专用的),直接删除该依赖项:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>4.1.0</version>
</dependency>

4. 验证配置加载有效性

启动客户端时添加调试日志,确认数据源属性是否被正确加载:

logging:
  level:
    org.springframework.core.env: DEBUG

查看日志中是否有spring.datasource.url等属性的加载记录,确认配置已生效。

5. 可选:添加DataSource初始化条件

如果以上步骤仍未解决,可在启动类上添加条件注解,确保只有当数据源属性存在时才初始化DataSource:

@SpringBootApplication
@ConditionalOnProperty(prefix = "spring.datasource", name = "url")
public class AdminApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApiApplication.class, args);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 21:25:02