在使用SpringBoot自动注解,如下,idea提示找不到CatalogService这个bean
@Autowired
CatalogService catalogService;
报错信息如下:
019-03-12 16:49:37.191 ERROR 8492 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field catalogService in top.bowen.controller.IndexController required a bean of type 'top.bowen.service.CatalogService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'top.bowen.service.CatalogService' in your configuration.
Process finished with exit code 0
大概意思就是找不到top.bowen.service下面的CatalogService,想啦很久我路径下面也有这个文件,为啥找不到呢。
原来,我们在使用top.bowen.service下面的CatalogService时,缺少了注解@Service,@Service注解帮助我们声明CatalogService时是一个bean,这点很重要,因为CatalogService是一个bean,其他的类才可以使用@Autowired将CatalogService作为一个成员变量自动注入,不然就会找不到