Spring Data MongoDB + Spring Boot 启动两次?

标签 spring spring-boot spring-data spring-data-mongodb

我正在尝试使 Spring Data MongoDB(响应式(Reactive))与 Spring Boot 2.1.5(和 WebFlux)一起使用。

从启动日志中我怀疑出了什么问题,它似乎被初始化了两次(注意两次存储库扫描,它们甚至返回不同的结果):

2019.05.19 11:59:53 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
2019.05.19 11:59:55 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 1235ms. Found 8 repository interfaces.
2019.05.19 11:59:55 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data repositories in DEFAULT mode.
2019.05.19 11:59:55 | INFO | org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 64ms. Found 0 repository interfaces.
2019.05.19 11:59:59 | INFO | org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019.05.19 11:59:59 | INFO | org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019.05.19 11:59:59 | INFO | org.mongodb.driver.cluster - Adding discovered server localhost:27017 to client view of cluster
2019.05.19 12:00:00 | INFO | org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:13}] to localhost:27017
2019.05.19 12:00:00 | INFO | org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 9]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=16058831}
2019.05.19 12:00:00 | INFO | org.mongodb.driver.cluster - Discovered cluster type of STANDALONE
2019.05.19 12:00:00 | INFO | org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:14}] to localhost:27017
2019.05.19 12:00:00 | INFO | org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 9]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=43838433}

我的构建文件中没有什么特别的:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-security")

    ...

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.security:spring-security-test")
}

绝对没有关于 Mongo 的 Java 配置,只有 application.properties 设置:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=mydb
spring.data.mongodb.username=myuser
spring.data.mongodb.password=mypass

我做错了什么?

最佳答案

我遇到了同样的症状。看来我有一个实现 AbstractReactiveMongoConfiguration 的 Configuration 类,这导致 MongoClient 被创建了两次。将客户端创建留给框架并通过属性对其进行配置解决了我的案例中的问题。

这是使用 Spring Boot 2.1.6-RELEASE 进行测试的,具有与海报相同的依赖项集。

关于Spring Data MongoDB + Spring Boot 启动两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206777/

相关文章:

javascript - 通过 Axios 发送 post 请求会在 Spring-Boot 后端生成一个空的 RequestBody。在 Postman 中工作,但不能通过 Axios 发布请求

java - Spring Boot编码问题ISO而不是UTF-8

具有不同参数的 Spring data JPA 查询

Spring 数据预测

java - 是否可以使用 Spring Data 和 MongoDB 的索引功能从子类中索引父类(super class)的公共(public)字段?

java - 无法通过反射 getter 和查询来获取字段值以在纯 Hibernate 中对多个列求和

java - spring-security 登录?注销重定向到登录

java - 更新 JHipster 后不再提供 Angular 组件,仅提供 root index.html

java - 为什么java将double转换为指数形式

java.lang.IllegalArgumentException : Not a managed type in spring boot app