我正在开发一个项目,我使用@Autowire将多个存储库加载到服务类中。服务类在项目C中,而我需要使用的存储库位于另外两个项目中,即项目A和项目B.我最初得到的错误是:
Field userRepo in com.example.service.UserService required a bean of type 'com.example.users.persistence.repository.UserRepository' that could not be found.
UserRepository位于项目A中。 此错误的解决方案是将@ComponentScan(" com.example")添加到控制器类。在修复了其他一些错误之后,我在另一个存储库中遇到了同样的错误。
Field tenantRepo in com.example.service.UserService required a bean of type 'com.example.commons.persistence.repository.TenantRepository' that could not be found.
TenantRepository位于项目B中。 问题是项目A和B都位于包" com.example"下,其中A位于" com.example.users",B位于" com下.example.commons"
在这种情况下,@ ComponentScan(" com.example")注释应该修复了这两个错误,但由于某种原因,项目B中的存储库未加载。 对于项目B中的所有存储库都会发生这种情况,如果我向@Autowired TenantRepository添加(required = false)[我不想在需要时执行此操作],我从B的下一个存储库得到相同的错误。包含服务类的项目C位于" com.example.users"。
我尝试了使用basePackageClasses的@ComponentScan,多包声明的不同变体;但我仍然得到同样的错误。在这种情况下会出现什么问题?是否有使用注释的解决方案(因为整个项目都基于它们,如果可能的话我真的不想使用xml)?