当前位置: 首页 > 知识库问答 >
问题:

未指定Spring引导失败的'url'属性

查飞星
2023-03-14

应用yml在带有主类和hibernate的模块中:

## JDBC part
spring:
  config:
    activate:
      on-profile: local
  datasource:
    url: jdbc:mysql://localhost:3306/kazi?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: emo
    password: 123qwe
  jpa:
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL8Dialect
    hibernate:
      ddl-auto: update
  liquibase:
    change-log: classpath:liquibase/changelog.xml
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

主要pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.emo.kazi</groupId>
        <artifactId>kazi-parent</artifactId>
        <version>1.2-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <artifactId>kazi</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>kazi-core</module>
        <module>kazi-api</module>
        <module>kazi-service</module>
    </modules>

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <skip>true</skip>
          </configuration>
        </plugin>
      </plugins>
    </build>
</project>

我的模块pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.emo.kazi</groupId>
        <artifactId>kazi</artifactId>
        <version>0.1-SNAPSHOT</version>
    </parent>

    <artifactId>kazi-core</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <name>kazi-core</name>
    <url>http://maven.apache.org</url>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
</project>

在父项目中:

<?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 http://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>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.emo.kazi</groupId>
    <artifactId>kazi-parent</artifactId>
    <version>1.2-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- GrantedAuthority wird in kazi project bei Role verwendet -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-plugin-releases</id>
            <url>https://repo.spring.io/plugins-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

错误:

2022-01-24 16:13:42.658  WARN 24223 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [liquibase.integration.spring.SpringLiquibase]: Factory method 'liquibase' threw exception; 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
2022-01-24 16:13:42.659  INFO 24223 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-01-24 16:13:42.761  INFO 24223 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-01-24 16:13:42.781 ERROR 24223 --- [  restartedMain] 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).


Process finished with exit code 0

共有1个答案

唐阳晖
2023-03-14

通过工厂方法的Bean实例化失败;嵌套异常org.springframework.beans.BeanInstantiationExc0019:未能实例化[com.zaxxer.hikari.HikariDataSource]:工厂方法'dataSource'抛出异常;嵌套异常org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationExc0019:未能确定合适的驱动程序类

stacktrace表示,在创建HikariDataSourcebean时会发生异常,因为它抱怨您没有在属性文件中为JDBC连接定义driverClassNameurl

但是创建这个HikariDataSourcebean是由DataSourceAutoConfiguration触发的,您已经在属性文件中明确排除了它。

因此,我认为可能存在一些配置问题,导致它无法加载应用程序。yml成功(可能是您放错文件夹了?)因为如果可以成功加载,它将排除配置的DataSourceAutoConfiguration,不会创建HikariDataSource,也不会引发此异常。

 类似资料:
  • 我的spring boot项目不需要datasource配置,但当我运行它时,出现了错误: 申请启动失败 描述:配置数据源失败:未指定“url”属性,无法配置嵌入式数据源。 原因:无法确定合适的驱动程序类别 行动: 考虑以下内容:如果您想要一个嵌入式数据库(H2、HSQL或Derby),请将其放在类路径上。如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有活动的配置文件) 我的a

  • 一直致力于使用Spring开发微服务。我已经成功地写了几个,但是,最后一个失败了,我不知道为什么。 我的申请。属性: (别担心,密码包含在application.properties) 已尝试添加和删除: Spring自动配置。排除=组织。springframework。靴子自动配置。jdbc。数据源自动配置 我的pom。xml 我的主要: @SpringBootApplication公共类Aut

  • 我正在开发一个用户管理API在一个Spring启动,我做了最需要的实体类,服务和存储库,但我错过了一些我看不到的东西。我想出了我的pom.xml执行我的主类,但它找不到它。当我运行"./mvnw sping-引导:运行"在我的项目目录下,它抛出了下面的构建失败: 我的pom.xml: 请,我会很高兴与任何帮助!谢谢

  • 我的项目昨天运行得很好,但今天我运行它时它突然报告了一个错误。 2019-06-04 19:09:57.206信息18231---[restartedMain]条件评估报告日志监听器: 启动应用程序上下文时出错。要显示条件报告,请在启用调试的情况下重新运行应用程序。2019-06-04 19:09:57.207ERROR 18231 --- [ restartedMain]o. s. b. d.

  • 在我的项目中有2个资源属性 1.application.properties 2. 应用开发.性能 此类使用开发的值属性 我使用 application-development.properties 作为我的项目资源 因此,我使用以下命令运行该项目: < code > mvn spring-boot:run-D spring . profiles . active = development 但是