启动springboot时提示找不到自动注入的对象,Field articlesService in com.bowen.web.controller.ApiArticleController required a bean of type ‘com.bowen.service.service.ArticlesService’ that could not be found. 具体如下:

  1. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2. 2019-10-21 21:53:42.871 ERROR 10704 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
  3. ***************************
  4. APPLICATION FAILED TO START
  5. ***************************
  6. Description:
  7. Field articlesService in com.bowen.web.controller.ApiArticleController required a bean of type 'com.bowen.service.service.ArticlesService' that could not be found.
  8. The injection point has the following annotations:
  9. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  10. Action:
  11. Consider defining a bean of type 'com.bowen.service.service.ArticlesService' in your configuration.
  12. Process finished with exit code 1

分析

检查该接口的实现

发现已经添加了@Service的声明,那么不可能是这个

  1. package com.bowen.service.service.Impl;
  2. import com.bowen.dao.ArticlesDao;
  3. import com.bowen.dao.model.Articles;
  4. import com.bowen.dao.mapper.ArticlesMapper;
  5. import com.bowen.service.service.ArticlesService;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. /**
  11. * <p>
  12. * 文章表 服务实现类
  13. * </p>
  14. *
  15. * @author wangbowen
  16. * @since 2019-10-19
  17. */
  18. @Service
  19. public class ArticlesServiceImpl implements ArticlesService {
  20. @Autowired
  21. private ArticlesDao articlesDao;
  22. @Override
  23. public Articles getArticleInfo(Integer id) {
  24. if(id <= 0){
  25. return null;
  26. }
  27. return articlesDao.getArticleInfo(id);
  28. }
  29. @Override
  30. public int addArticleViewCount(Integer id) {
  31. if(id <= 0){
  32. return 0;
  33. }
  34. return articlesDao.addArticleViewCount(id);
  35. }
  36. @Override
  37. public int addPraiseCount(Integer id) {
  38. return 0;
  39. }
  40. @Override
  41. public List<Articles> queryApiArticlesList(Integer type, String title, Integer page, Integer pageSize) {
  42. return null;
  43. }
  44. @Override
  45. public List<Articles> hotArticlesList() {
  46. return articlesDao.hotArticlesList();
  47. }
  48. }

检查springboot启动文件BoboServiceApplication

  1. 发现没有包含ComponentScan,由于我这个项目是分模块的项目,启动文件和调用的service不在同一个包,那么可能扫描不到包,导致无法找到自动注入的类。
  2. package com.bowen.service;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6. @SpringBootApplication
  7. public class BoboServiceApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(BoboServiceApplication.class, args);
  10. }
  11. }

解决方案

BoboServiceApplication 加入如下代码

  1. package com.bowen.service;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ComponentScan;
  5. /**
  6. * 加入需要扫描的包地址,防止出现找不到注入对象的问题
  7. */
  8. @ComponentScan(basePackages = {"com.bowen.*"})
  9. @SpringBootApplication
  10. public class BoboServiceApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(BoboServiceApplication.class, args);
  13. }
  14. }

总结

  • ComponentScan做的事情就是告诉Spring从哪里找到bean
  • 如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
  • 如果你有一些bean所在的包,不在main app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包