问题描述
我遇到了一个我无法理解的问题:
***************************
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
。
在您的主类中,用@Configuration
,EnableAutoConfiguration
和@ComponentScan({"com.service.applicant","your.admin.controller.path"})
注释对象。
RequestController
中的用Applicant
注释类型@Autowire
的属性。
当您不显式使用@ComponentScan时,主类上的@SpringBootApplication批注中将实现@ComponentScan(以及@EnableAutoConfiguration)。因此,只要所有标记为@component(或类似批注,如控制器,服务等)的类都位于定义主类的相同或子包中,就会自动进行扫描。
现在,在您的情况下,在主类中添加了// @ ComponentScan(basePackageClasses = AdminController.class)。这样做的结果是,它仅为类AdminController创建了bean,而忽略了所有其他类。