SpringBoot Bean无法注入的问题 org.springframework.beans.factory.NoSuchBeanDefinitionException

整个项目通过Maven构建,大致结构如下:

 

 

 

其中在 screenrecord 有一个Application类

package com.zzz.screenrecord;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author wang
 * create on 2020/7/2
 */
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ScreenRecordApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScreenRecordApplication.class,args);
    }
}

运行之后 总是报错:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zzz.screenrecord.mapper.ScreenRecordMapper' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]

 

后来经研究发现,SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!
“Application类”是指SpringBoot项目入口类。这个类的位置很关键:

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

回答: 这个错误org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.web.client.RestTemplate是由于在Spring容器中找不到对应的RestTemplate bean引起的。根据你提供的代码和更改后的代码,我猜测你可能没有在bean6.xml配置文件中定义名为'org.springframework.web.client.RestTemplate'的bean。要解决这个问题,你需要在bean6.xml中添加一个RestTemplate的bean定义,以便让Spring容器能够正确地找到并实例化该bean。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决报错问题org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ...](https://blog.csdn.net/qq_58372242/article/details/124501799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring学习-org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘myBean...](https://blog.csdn.net/d347091231/article/details/120745350)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值