这是我第一次将MongoDB与Spring结合使用,我想做的就是启动Spring服务器,但是连接打开后会立即关闭。
环顾类似的场景后,有人提出了删除Spring Data JPA依赖项的建议,我没有但没有帮助。另一个建议是说要添加h2作为依赖项,因为Spring Starter在做一些事情之前就需要它,这确实有所帮助,因为以前的连接甚至都没有打开。我还编辑了属性文件,但似乎没有任何效果。
成绩文件:
group = 'com.danielturato'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
application.properties:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mongo
server.port=8095
日志:
2019-08-22 00:04:13.109 INFO 16376 --- [ main] com.danielturato.esports.Application : Starting Application on DESKTOP-E2MJ251 with PID 16376 (started by danie in D:\Projects\eSports Project)
2019-08-22 00:04:13.111 INFO 16376 --- [ main] com.danielturato.esports.Application : No active profile set, falling back to default profiles: default
2019-08-22 00:04:13.368 INFO 16376 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-08-22 00:04:13.411 INFO 16376 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 39ms. Found 1 repository interfaces.
2019-08-22 00:04:14.117 INFO 16376 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-08-22 00:04:14.117 INFO 16376 --- [ main] org.mongodb.driver.cluster : Adding discovered server localhost:27017 to client view of cluster
2019-08-22 00:04:14.150 INFO 16376 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:14}] to localhost:27017
2019-08-22 00:04:14.153 INFO 16376 --- [localhost:27017] 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, 2, 0]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1950400}
2019-08-22 00:04:14.154 INFO 16376 --- [localhost:27017] org.mongodb.driver.cluster : Discovered cluster type of STANDALONE
2019-08-22 00:04:14.378 INFO 16376 --- [ main] com.danielturato.esports.Application : Started Application in 1.482 seconds (JVM running for 1.99)
2019-08-22 00:04:14.414 INFO 16376 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:15}] to localhost:27017
2019-08-22 00:04:14.429 INFO 16376 --- [ Thread-2] org.mongodb.driver.connection : Closed connection [connectionId{localValue:2, serverValue:15}] to localhost:27017 because the pool has been closed.
如果有人有其他建议,将不胜感激!