Spring Boot 调用 MongoRepository时报org.springframework.beans.factory.NoSuchBeanDefinitionException错误的解决...

这个问题整整折腾了我两天,现在记录下来,希望可以帮助和我一样,遇到相同问题的小伙伴。

项目是分层的(Intellij IDEA中的模块Module),有API(Core)层,Service&Dao,Common,Model,上一张项目结构图。(不要在意为什么Service和Dao放在一起。这不是重点。)

API中包括了Controller,Mongodb在Service&Dao层中,也就是API层,调用Service&Dao层,我敢用性命担保,所有注释,逻辑,都没有错,但是运行SpringBoot的时候,偏偏报错。(找不到依赖,就是注入失败。)

代码中继承了MongoRepository接口

public interface IUserGroupRepository extends MongoRepository<UserGroup, Integer> {
    UserGroup findById(Integer id);
}

 报错内容如下:

2017-06-28 10:11:52.423  WARN 8648 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userGroupController': Unsatisfied dependency expressed through field 'userGroupRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.iboxpay.nosql.mongodb.IUserGroupRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2017-06-28 10:11:52.426 INFO 8648 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-06-28 10:11:52.446 INFO 8648 --- [ main] utoConfigurationReportLoggingInitializer : Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-06-28 10:11:52.591 ERROR 8648 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field userGroupRepository in com.iboxpay.ops.api.controller.UserGroupController required a bean of type 'com.iboxpay.nosql.mongodb.IUserGroupRepository' that could not be found. Action: Consider defining a bean of type 'com.iboxpay.nosql.mongodb.IUserGroupRepository' in your configuration.

  解决办法如下:

@SpringBootApplication(scanBasePackages = {"com.iboxpay.service", "com.iboxpay.ops.api", "com.iboxpay.nosql"})
@EnableMongoRepositories(basePackages = {"com.iboxpay.nosql"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

因为我的项目是分层的,SpringBoot的@SpringBootApplication注解要添加扫描包,否则有的地方可能会注入失败。另外一点比较重要,需要使有和Mongodb的@EnableMongoRepositories注解,并指定要扫描的包(IUserGroupRepository所在的包,即继承

MongoRepository接口的接口中所在的包。)
上面两个com.iboxpay.nosql缺一不可。切记,切记。

 

转载于:https://www.cnblogs.com/zhuiyi/p/7088558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值