• springboot available: expected at least 1 bean which qualifies as autowire candidate奇葩问题


    Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiUserController': Unsatisfied dependency expressed through field 'apiMutiHttpClientUtil'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.crt.openapi.utils.ApiMutiHttpClientUtil' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

     Description:

    Field apiMutiHttpClientUtil in com.crt.openapi.apiinterface.controller.base.CommonController required a bean of type 'com.crt.openapi.utils.ApiMutiHttpClientUtil' that could not be found.

    The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

    Action:

    Consider defining a bean of type 'com.crt.openapi.utils.ApiMutiHttpClientUtil' in your configuration.

     总是提示无法注入Service或者Dao中的Bean!

    后来经研究发现,SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!

     “Application类”是指SpringBoot项目入口类。这个类的位置很关键:

     如果Application类所在的包为:com.demo.module,则只会扫描com.demo.module包及其所有子包,如果service或dao所在包不在com.demo.module及其子包下,则不会被扫描!

     所以需在application启动类中加上

     @MapperScan("com.*.dao")//加上你项目的dao或service所在文件位置即可

    @SpringBootApplication

  • 相关阅读:
    MongoDB存储时间
    如何在博客园随笔中增加章节导航
    如何优雅地从CSDN转载文章
    线段树详解(原理、实现与应用)
    Codeforces 1076D——最短路算法
    顶点支配、独立与覆盖
    CodeForces
    数据结构一——顺序表
    平面图的基本概念及性质
    编程之美——一摞烙饼的排序(暴搜+剪枝)
  • 原文地址:https://www.cnblogs.com/hikoukay/p/11577961.html
Copyright © 2020-2023  润新知