当前位置:   article > 正文

使用MongoRepository对数据进行增删改查,出现java.lang.IllegalArgumentException: MongoOperations 必须不能为null

java.lang.illegalargumentexception: mongooperations must not be nul

使用MongoRepository对数据进行增删改查,出现java.lang.IllegalArgumentException: MongoOperations must not be null!异常,MongoOperations 不能为空。  

原因:百度MongoOperations 类是做什么的,发现MongoTemplate类实现MongoOperations类

image-20211103155627881

在使用MongoRepository时,程序也是借助MongoOperations 完成对应的操作,可以MongoRepository工厂构造会传入MongoOperations 类,获取它的上下文context。

image-20211103160033404

看出报错MongoOperations must not be null!,说明MongoOperations 是空的,想一下程序也没有过多的配置,无非xml中的配置,所以我们的目光转向配置文件,与MongoRepository相关的就这一行配置,标识我的repositories位置在哪里。

<mongo:repositories base-package="com.digiwin.app.ttt.service.mongo.repository" repository-impl-postfix="repository" />

然后MongoOperations 为空,那说明MongoTemplate也为空,而MongoTemplate模板的如下,也没有太大的问题

  1. <!-- mongodb的模板 -->
  2.    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  3.        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
  4.    </bean>

所以repositories中需要把MongoTemplate模板引入进去,那肯定是少两者关联(mongo-template-ref="mongoTemplate")的配置了,最终的代码为

  1. <!--   mongo-template-ref="mongoTemplate" 要加,不然MongoRepository用不了,血泪史-->
  2. <mongo:repositories base-package="com.digiwin.app.ttt.service.mongo.repository"
  3.                    mongo-template-ref="mongoTemplate" repository-impl-postfix="repository"/>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/56008
推荐阅读
相关标签
  

闽ICP备14008679号