整个项目通过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包下,则不会被扫描。
本文深入探讨了SpringBoot项目中Bean装配失败的问题,详细解释了Application类所在包位置对扫描范围的影响,以及如何确保service和dao等组件能被正确扫描。
3423

被折叠的 条评论
为什么被折叠?



