Spring Boot启动失败:无法配置DataSource,无法确定合适驱动类
Spring Boot启动数据源配置失败问题排查
错误日志
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2022-12-27T22:34:33.521+05:30 ERROR 19798 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: 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 Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
我的application.yaml配置
#database config:mysql spring.datasource.url=jdbc:mysql://localhost:3306/27Dec spring.datasource.username=root spring.datasource.password=mysql@17 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #hibernate config spring.jpa.hibernate.ddl-auto = validate spring.jpa.show-sql = true spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect #when serilizing it wont restrict if fields are null or naything.... spring.jackson.serialization.fail-on-empty-beans=false spring.sql.init.platform=mysql spring.sql.init.schema-locations=schema-mysql.sql spring.sql.init.data-locations=data-mysql.sql spring.datasource.initialize=true spring.sql.init.continue-on-error=true
我的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springrest</groupId> <artifactId>springrest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springrest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> <!-- use UTF-8 for everything --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <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> <scope>provided</scope> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
已确认目标数据库及对应数据表已创建,试过Stack Overflow上的常规方案但无效,求解决该问题的思路。
解决思路
修复application.yaml格式错误:YAML格式要求用
键: 值(冒号后加空格)的写法,而非等于号。正确配置示例:spring: datasource: url: jdbc:mysql://localhost:3306/27Dec username: root password: mysql@17 driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: validate show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL8Dialect naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl jackson: serialization: fail-on-empty-beans: false sql: init: platform: mysql schema-locations: schema-mysql.sql data-locations: data-mysql.sql continue-on-error: true注:
spring.jpa.hibernate.naming-strategy在Spring Boot 2.x+已废弃,需改用physical-strategy或implicit-strategy。修正JPA依赖的scope:
spring-boot-starter-data-jpa的scope设为provided会导致运行时无法加载JPA核心类,移除该scope标签,使用默认的compile范围:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>替换兼容的JAXB依赖:Spring Boot 3.0.x基于Jakarta EE API,原
javax.xml.bind属于Java EE,需替换为Jakarta版本:<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>4.0.0</version> <scope>runtime</scope> </dependency>验证数据库连接可用性:确认MySQL服务正在运行,端口3306正常开放,用户名密码正确,数据库
27Dec确实存在。启用debug模式排查:启动应用时添加
--debug参数,查看条件评估报告,确认数据源配置是否被正确加载。
内容的提问来源于stack exchange,提问作者Ritik Sharma
相关产品推荐
相关产品推荐

