无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。Gradle 项目中的错误

Posted

技术标签:

【中文标题】无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。Gradle 项目中的错误【英文标题】:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Error In Gradle project 【发布时间】:2020-12-17 23:20:32 【问题描述】:

我创建了一个新的 Spring Boot Gradle 项目。我还没有编码。但是在以 Spring Boot 方式运行应用程序时出现错误。我需要在这里添加任何其他依赖项吗?或者有什么解决办法?

build.gradle

plugins 
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'


group = 'com.informationbook'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories 
    mavenCentral()


dependencies 
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') 
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    
    testImplementation 'org.springframework.batch:spring-batch-test'


test 
    useJUnitPlatform()

这里我包括 控制台 以供参考。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-08-29 10:07:44.398  INFO 18616 --- [           main] c.i.InformationBookApplication           : Starting InformationBookApplication on LAPTOP-I4F37P4O with PID 18616 (C:\Users\mitad\Documents\STS_Workspace\Information-Book\bin\main started by mitad in C:\Users\mitad\Documents\STS_Workspace\Information-Book)
2020-08-29 10:07:44.400  INFO 18616 --- [           main] c.i.InformationBookApplication           : No active profile set, falling back to default profiles: default
2020-08-29 10:07:44.892  INFO 18616 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-29 10:07:44.897  INFO 18616 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-29 10:07:44.897  INFO 18616 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-29 10:07:44.945  INFO 18616 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-29 10:07:44.945  INFO 18616 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 523 ms
2020-08-29 10:07:45.040  INFO 18616 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-29 10:07:45.114  WARN 18616 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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
2020-08-29 10:07:45.114  INFO 18616 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-08-29 10:07:45.116  INFO 18616 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-08-29 10:07:45.123  INFO 18616 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-29 10:07:45.124 ERROR 18616 --- [           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).

【问题讨论】:

【参考方案1】:

请运行 Gradle 依赖并检查依赖树,控制台将打印

Run gradle dependency , console will print your dependency tree,

+--- org.springframework.boot:spring-boot-starter-batch -> 2.3.3.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:2.3.(*)
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    |    +--- org.springframework:spring-core:5.2.8.RELEASE (*)
|    |    \--- org.yaml:snakeyaml:1.26
|    +--- org.springframework.boot:spring-boot-starter-jdbc:2.3.3.RELEASE

Spring batch starter 对 Jdbc starter 有依赖关系,您可以提供嵌入式 DB 依赖关系,也可以在应用程序属性中添加数据库属性

如果您不想依赖数据库,只需排除自动配置 @SpringBootApplication(exclude=DataSourceAutoConfiguration.class)

【讨论】:

以上是关于无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。Gradle 项目中的错误的主要内容,如果未能解决你的问题,请参考以下文章

无法配置数据源:未指定“url”属性,并且无法配置嵌入式数据源。原因:发

无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。- java spring 项目

数据源:未指定“url”属性,无法配置嵌入式数据源

无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。Gradle 项目中的错误

无法确定合适的驱动程序类弹簧

Liquibase ERROR 数据库 URL 未指定为参数或属性文件