SpringBoot 管理引起的冲突

问题 一个接口继承了 MongoRepository ,启动项目可能会出现(如下错误),什么意思?就是你已经在spring里注册过这个类了,现在又要注册,重复注册。

   

    The bean 'jncCurrentStageDataDao', defined in com.lyg.rtt.dao.JncCurrentStageDataDao defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in file [D:\gitee\pro202009\target\classes\com\lyg\rtt\dao\JncCurrentStageDataDao.class] and overriding is disabled.

   

   

// 这是继承MongoRepostory的接口

public interface JncCurrentStageDataDao extends MongoRepository<JncCurrentStageDataEntity , String> {

}

  

   

那么,为什么会重复注册?

   

就是看看你的 SpringBoot集成的mybatis ,扫描的是哪些包,(或者是其它框架扫描的包,一般都是mybatis)

   

你会发现 果不其然: 重复注册的类 会被mybatis框架 扫描到,而且会交给spring来管理。

   

而你的这个类又继承了 MogoRepository ,也会被spring 管理。

   

   

解决这个问题的办法:

   

①控制好 mybatis的扫描范围。

   

②为继承 MogoRepository 的接口新建一个独立的包。

   

posted @ 2020-11-24 17:57  黑质白章  阅读(739)  评论(1编辑  收藏  举报