问题-@ComponentScanbasePackageClasses = AdminController.class<-为什么它破坏了我的应用程序?

问题描述

我遇到了一个我无法理解的问题:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.


Action:

Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.

我已经做了一些研究,但是当我只是从我的主应用程序类中删除@ComponentScan并起作用时,我仍然无法解决此问题。

我删除了:

//@ComponentScan(basePackageClasses = AdminController.class)

一切都很好,但是我很好奇...有人可以帮我吗?

解决方法

您应该先用Applicant注释类@Component。 在您的主类中,用@ConfigurationEnableAutoConfiguration@ComponentScan({"com.service.applicant","your.admin.controller.path"})注释对象。

RequestController中的

Applicant注释类型@Autowire的属性。

,

当您不显式使用@ComponentScan时,主类上的@SpringBootApplication批注中将实现@ComponentScan(以及@EnableAutoConfiguration)。因此,只要所有标记为@component(或类似批注,如控制器,服务等)的类都位于定义主类的相同或子包中,就会自动进行扫描。

现在,在您的情况下,在主类中添加了// @ ComponentScan(basePackageClasses = AdminController.class)。这样做的结果是,它仅为类AdminController创建了bean,而忽略了所有其他类。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...