我正在用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)


当前回答

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

其他回答

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

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

只需添加:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 对我有用。

我得到相同的错误,我尝试@EnableAutoConfiguration(排除=…)没有工作。

对于那些想知道在哪里添加@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})的人,因为它已经被用户问到了。你需要把它添加到主应用程序类,它在src>main>java下。默认情况下,它被设置为@SpringBootApplication

向pom文件添加h2依赖项可以解决这些问题。 ...... com.h2database h2 ......

当通过Spring Initializr创建一个项目时,你的资源目录可能没有添加到类路径中。所以你的应用永远不会加载这个应用。已配置的属性文件。

如果是这种情况,请将以下内容添加到应用程序中进行快速测试。属性文件:

server.port=8081

现在,当运行你的应用程序时,你应该在spring引导控制台中看到类似这样的输出:

INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): **8081** (http) with context path ''

如果您的端口仍然是默认的8080,而没有更改为8081,则您的应用程序。属性文件显然没有加载。

你也可以从命令行检查你的应用程序是否使用gradle bootRun运行。这很可能是工作。

解决方案:

关闭IntelliJ,然后在项目文件夹中删除“。想法”文件夹 重新导入您的项目到IntelliJ,如下所示:“导入项目”->“选择仅您的构建。Gradle文件导入”。(IntelliJ会自动抓取剩下的) 重新构建并运行应用程序

详见IntelliJ Support的官方回答: 想法- 221673

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

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