我正在用MongoDB做一个Spring Boot Batch示例,我已经启动了mongod服务器。

当我启动我的应用程序时,我得到下面的错误。

对这个问题有什么建议吗?

***************************
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.properties:

# Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.uri=mongodb://localhost/test 

pom.xml

<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>

我已经启动了mongod,输出如下:

C:\Users\pc>mongod
2018-07-07T14:39:39.223+0530 I JOURNAL  [initandlisten] journal dir=C:\data\db\journal
2018-07-07T14:39:39.230+0530 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2018-07-07T14:39:39.478+0530 I JOURNAL  [durability] Durability thread started
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] MongoDB starting : pid=11992 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-NQ639DU
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-07-07T14:39:39.591+0530 I CONTROL  [initandlisten] db version v3.0.5
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2018-07-07T14:39:39.593+0530 I CONTROL  [initandlisten] options: {}
2018-07-07T14:39:39.595+0530 I JOURNAL  [journal writer] Journal writer thread started
2018-07-07T14:39:40.485+0530 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-07-07T14:40:39.140+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51340 #1 (1 connection now open)
2018-07-07T14:40:41.663+0530 I NETWORK  [conn1] end connection 127.0.0.1:51340 (0 connections now open)
2018-07-07T14:45:12.421+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51578 #2 (1 connection now open)
2018-07-07T14:45:12.870+0530 I NETWORK  [conn2] end connection 127.0.0.1:51578 (0 connections now open)
2018-07-07T14:46:21.734+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51591 #3 (1 connection now open)
2018-07-07T14:46:22.041+0530 I NETWORK  [conn3] end connection 127.0.0.1:51591 (0 connections now open)
2018-07-07T14:57:47.523+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:52534 #4 (1 connection now open)
2018-07-07T14:57:47.910+0530 I NETWORK  [conn4] end connection 127.0.0.1:52534 (0 connections now open)


当前回答

这不是什么大问题,只是付出 在Application.properties中添加: ' '

spring.datasource.name = /名称/

其他回答

" Failed to configure a DataSource "错误。首先,我们通过定义数据源来解决这个问题。接下来,我们讨论了如何在不配置数据源的情况下解决这个问题。

https://www.baeldung.com/spring-boot-failed-to-configure-data-source

如果数据源是在应用程序中定义的。资源,确保它位于src/main下面,并将其添加到构建路径。

这可能是因为您有jpa依赖关系和插件……

如果不使用(build.;)Gradle或pom文件)

e . g。

// kotlin("plugin.jpa") version "1.3.61"

// implementation("org.springframework.boot:spring-boot-starter-data-jpa")

您的问题是spring批处理spring-boot-starter-batch的依赖关系,它具有spring-boot-starter-jdbc传递maven依赖关系。

Spring Batch是一个用于构建可靠和容错的企业批处理作业的框架。它支持许多特性,如重新启动失败的批处理、记录批处理执行的状态等等。为了实现此目的,Spring Batch使用数据库模式存储已注册作业的状态,自动配置已经为您提供了所需数据源的基本配置,并且正是此配置需要关系数据库配置。

要解决这个问题,你必须包括一些数据库驱动程序,如mysql, h2等来配置url。

更新: 作为开始,您可以配置应用程序。Yml如下:

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
    username: admin
    password:

当然,在你的pom.xml中包括这样的h2驱动程序:

<?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>

The motivation, because you can not use mongo for this purpose, is that the usage of mongo is provided only for item readers and writers and not for managing the internal database of Spring Batch that is an internal schema, not a business schema. The query is plain SQL query and the internal abstraction relies on a relational database. It is necessary to have a database with ACID capability because every batch reads and writes a chunk of work and saves that information in order to restart the job. A NoSql solution is not suitable for this.

最后,您已经配置了一个关系数据库,以便为Spring Batch的内部功能做好准备,内部抽象不依赖于mongo,只依赖于jdbc。然后mongo可以通过项目读取器/写入器用于批处理的业务方面。

我希望这能帮助你消除疑虑。

根本原因

JPA (Java持久性API)是ORM(对象-关系映射)工具的Java规范。spring-boot-starter-data-jpa依赖项在spring引导框架的上下文中启用ORM。

spring引导应用程序的JPA自动配置特性尝试使用JPA Datasource建立数据库连接。JPA DataSource bean需要数据库驱动程序连接到数据库。

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

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

application.properties

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

application.yaml

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

通过编程

@SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })