Unsatisfied dependency expressed through field 'mongoTemplate'
具体情况是我的聚合项目,在使用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <!– <version>2.1.5.RELEASE</version>–> </dependency>
遇到报错,大概就是连接linux 在一段时间,大概8分钟左右,会自动断开,下次第一次刷新会报错,之后就会好了
2020-06-28 10:06:55.058 WARN 25076 --- [nio-9080-exec-2] org.mongodb.driver.connection : Got socket exception on connection [connectionId{localValue:8, serverValue:5252}] to 112.124.46.197:22017. All connections to 112.124.46.197:22017 will be closed. 2020-06-28 10:06:55.058 INFO 25076 --- [nio-9080-exec-2] org.mongodb.driver.connection : Closed connection [connectionId{localValue:8, serverValue:5252}] to 112.124.46.197:22017 because there was a socket exception raised by this connection. org.springframework.data.mongodb.UncategorizedMongoDbException: Exception sending message; nested exception is com.mongodb.MongoSocketWriteException: Exception sending message at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138) at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2774)
自己创建一个demo ,使用的高版本后,并没有此问题,可是老项目中,主依赖中我修改了mongodb的版本,问题依然存在,讲mongodb单纯分出一个项目后,这个问题可以解决.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
于是只能单独将mongo分出来了,在主项目中引入配置文件
此时启动会遇到问题:
Unsatisfied dependency expressed through field 'mongoTemplate'
疑惑,测试的demo里面也是这样写的,没有出现此问题,为何迁移到主项目中mongotemplate没有注入
原因:
去掉主项目的启动类中的两个mongoauto配置,就解决了这个问题.