在使用SpringBoot自动注解,如下,idea提示找不到CatalogService这个bean

  1. @Autowired
  2. CatalogService catalogService;

报错信息如下:

  1. 019-03-12 16:49:37.191 ERROR 8492 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
  2. ***************************
  3. APPLICATION FAILED TO START
  4. ***************************
  5. Description:
  6. Field catalogService in top.bowen.controller.IndexController required a bean of type 'top.bowen.service.CatalogService' that could not be found.
  7. The injection point has the following annotations:
  8. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  9. Action:
  10. Consider defining a bean of type 'top.bowen.service.CatalogService' in your configuration.
  11. Process finished with exit code 0

大概意思就是找不到top.bowen.service下面的CatalogService,想啦很久我路径下面也有这个文件,为啥找不到呢。
原来,我们在使用top.bowen.service下面的CatalogService时,缺少了注解@Service@Service注解帮助我们声明CatalogService时是一个bean,这点很重要,因为CatalogService是一个bean,其他的类才可以使用@Autowired将CatalogService作为一个成员变量自动注入,不然就会找不到