首页 > 解决方案 > SpringBootApplication scanBasePackages 不读取子包

问题描述

我在 nexus 上发布了一个具有包 ID 的通用库

x.xx.common

包含常用的feign客户端代理接口的子包

使用此库的项目具有包 ID。

x.xx.account 
x.xx.device

这些项目中的每一个在根目录中都有其应用程序类

x.xx.account.AppClass
x.xx.device.AppClass

这些课程中的每一个都有

@SpringBootApplication(scanBasePackages = {"x.xx"})

出于某种原因,两个项目都看不到子包下的任何代理接口

x.xx.common.proxy
x.xx.common.configuration

我尝试将代理接口直接移动到主包下

x.xx.common

但它也失败了

x.xx.common.service.impl.AuditServiceImpl 中构造函数的参数 0 需要找不到类型为“x.xx.common.LogProxy”的 bean。

每个接口代理都会出现该错误

标签: javaspring-boot

解决方案


向要扫描的子包类添加注释。@Component分别将,@Service或等注释@Repository分别添加到类中。对于注释: @SpringBootApplication(scanBasePackages = {"x.xx"})

假设Abc子包中有一个类命名x.xx,所以在该类中添加注解@Component。

@Component class Abc{}

这将有助于阅读子包类。

要详细了解上述注解之间的区别: Spring 中的@Component、@Repository 和@Service 注解有什么区别?


推荐阅读