当前位置 博文首页 > 文章内容

    Unsatisfied dependency expressed through field 'mongoTemplate'

    作者: 栏目:未分类 时间:2020-06-29 10:37:50

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    具体情况是我的聚合项目,在使用

    <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配置,就解决了这个问题.