Spring . 我将我的答案发布给未来的读者。以前它工作得很好,但不知怎的又恢复了。通过从pom.xml中删除一些不必要的插件和依赖项,我解决了这个问题

Spring . 我将我的答案发布给未来的读者。以前它工作得很好,但不知怎的又恢复了。通过从pom.xml中删除一些不必要的插件和依赖项,我解决了这个问题,spring,spring-boot,spring-batch,Spring,Spring Boot,Spring Batch,首先,我将默认打包类型更改为jar(Spring Boot初始值设定项在打包中提供pom) jar 我无意中添加了一些插件: maven战争插件 真的 target/web.xml src/main/webapp 真的 我希望我的回答能对别人有所帮助。这一条对我很有用,对MySQL来说: (应用程序属性) 可能是在通过Spring初始化器创建项目时,资源目录未添加到类路径。因此,您的应用程序永远不会加载您配置的application.properties文件 若要在这种情况下进行快速测试,请

首先,我将默认打包类型更改为jar(Spring Boot初始值设定项在打包中提供pom

jar

  • 我无意中添加了一些插件:

    
    maven战争插件
    真的
    target/web.xml
    src/main/webapp
    真的
    


  • 我希望我的回答能对别人有所帮助。

    这一条对我很有用,对MySQL来说: (应用程序属性)


    可能是在通过Spring初始化器创建项目时,资源目录未添加到类路径。因此,您的应用程序永远不会加载您配置的application.properties文件

    若要在这种情况下进行快速测试,请将以下内容添加到application.properties文件中:

    server.port=8081
    
    现在,在运行应用程序时,您应该在spring引导控制台中看到如下输出:

    <?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>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
           ....
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
            </dependency>
    
    ....
        </dependencies>
    ...
    
    </project>
    
    INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): **8081** (http) with context path ''
    
    如果您的端口仍然是默认的8080,并且没有更改为8081,则application.properties文件显然没有加载

    您还可以从命令行检查应用程序是否使用
    gradle bootRun
    运行。最有可能的是工作

    解决方案:

    # Mongo database URI. Cannot be set with host, port and credentials.
    spring.data.mongodb.uri=mongodb://localhost/test 
    
  • 关闭IntelliJ,然后在项目文件夹中删除“.idea”文件夹
  • 将项目重新导入IntelliJ,如下所示:“导入项目”->“仅选择要导入的build.gradle文件”。(IntelliJ将自动抓取其余部分)
  • 再次构建并运行应用程序
  • 请参阅IntelliJ Support的官方答复:
    不包括
    数据源自动配置类
    对我有用:

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
    

    我在pom.xml中删除了对mybatis的过时依赖,以使我的运行。

    这可能是因为您有jpa依赖项和插件

    如果不使用(build.gradle或pom文件),只需对其进行注释

    e。g


    我认为在导入模块时,您已经导入了另一个包,请转到模块并删除所有模块。然后从项目包导入模块

    如果您使用的是Gradle,则重建Gradle可以解决此问题。

    如果pom.xml中有JPA依赖项,则只需将其删除即可。这个解决方案适合我。

    在主java文件中添加此注释

    spring.datasource.url=jdbc:mysql://localhost:3306/rest
    spring.datasource.username=
    spring.datasource.password=
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.generate-ddl=true
    spring.jpa.hibernate.ddl-auto = update 
    
    @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
    

    如果您在pom.xml中添加了“spring boot starter data jpa”依赖项,请在依赖项中添加相应的数据库,如h2等。

    我在启动新项目时遇到相同的错误。使用命令行对我有用

    ./gradlew bootRun
    

    根本原因

    JPA(Java持久性API)是用于ORM(对象关系映射)工具的Java规范。SpringBootStarter数据jpa依赖在SpringBoot框架的上下文中启用ORM

    spring boot应用程序的JPA自动配置功能尝试使用JPA数据源建立数据库连接。JPA数据源bean需要数据库驱动程序连接到数据库

    数据库驱动程序应作为pom.xml文件中的依赖项提供。对于Oracle、SQL Server、MySql、DB2、Postgres、MongoDB等外部数据库,需要数据库JDBC连接属性来建立连接


    您需要配置数据库驱动程序和JDBC连接属性来修复此异常。配置数据源失败:“url”属性未指定,无法配置嵌入式数据源。原因:无法确定合适的驱动程序类别

    应用程序属性

    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 
    
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
    应用程序.yaml

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-batch</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    spring:
    autoconfigure:
        exclude:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
    通过编程

    @SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })
    

    若您使用YAML进行配置,那个么可能是缩进问题。彻底检查YAML文件。

    检查您的application.properties文件。 其中一个可能的原因是

    • 您可能添加了“SpringData”maven插件,但您没有 在application.properties文件中提供数据存储详细信息。

    您需要配置数据库驱动程序和JDBC连接属性来修复此异常。配置数据源失败:“url”属性未指定,无法配置嵌入式数据源。原因:无法确定合适的驱动程序类别

    应用程序属性

    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 
    
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    

    我在代码中遇到了同样的问题,在Application.java文件中添加这段代码帮助我解决了这个问题-


    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})

    作为最新2021 spring boot发行版的摘要

    如果您的application.properties中至少有这些条目

    spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&
    useLegacyDatetimeCode=false&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=admin
    spring.jpa.hibernate.ddl-auto=create
    spring.jpa.show-sql=true
    
    spring.datasource.url=jdbc:mysql://localhost:3306/db
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    以及pom.xml中的这些依赖项

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    就我而言,这是IDE 无论您使用的是eclipse还是intellij,应用程序都必须在真实环境中在linux上运行。因此,要验证是否存在IDE问题,请使用shell运行应用程序

    mvn spring-boot:run
    
    如果启动时没有错误,问题出在您的IDE中

    日食 在我的例子中,我在spring boot项目中的经典应用程序上用右键单击运行项目。java,然后作为java应用程序运行

    经过数小时的研究,解决方案是:

    右键单击根spring启动项目上的,然后作为java应用程序运行。Eclipse向我展示了几个带有主要方法的类。我选择Application.java,然后运行

    长篇大论
    如果您检查确切的方法错误,您将看到只需要driverClassName或dirver类名和url。

    这是我的问题-如果使用MongoDB,我们仍然需要使用H2吗?我用了H2,但问题还是一样!!我更新答案。
    server.port=8081
    
    INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): **8081** (http) with context path ''
    
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
    
    // kotlin("plugin.jpa") version "1.3.61"
    
    // implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    
    @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
    
    ./gradlew bootRun
    
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 
    
    spring:
    autoconfigure:
        exclude:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
    @SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })
    
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
    
    spring.datasource.url=jdbc:mysql://localhost:3306/db
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    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
    
    mvn spring-boot:run
    
    Eclipse IDE for Enterprise Java and Web Developers
    Version: 2021-03 (4.19.0)
    Build id: 20210312-0638