启动springboot时提示找不到自动注入的对象,Field articlesService in com.bowen.web.controller.ApiArticleController required a bean of type ‘com.bowen.service.service.ArticlesService’ that could not be found. 具体如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-21 21:53:42.871 ERROR 10704 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field articlesService in com.bowen.web.controller.ApiArticleController required a bean of type 'com.bowen.service.service.ArticlesService' 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 'com.bowen.service.service.ArticlesService' in your configuration.
Process finished with exit code 1
分析
检查该接口的实现
发现已经添加了@Service的声明,那么不可能是这个
package com.bowen.service.service.Impl;
import com.bowen.dao.ArticlesDao;
import com.bowen.dao.model.Articles;
import com.bowen.dao.mapper.ArticlesMapper;
import com.bowen.service.service.ArticlesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 文章表 服务实现类
* </p>
*
* @author wangbowen
* @since 2019-10-19
*/
@Service
public class ArticlesServiceImpl implements ArticlesService {
@Autowired
private ArticlesDao articlesDao;
@Override
public Articles getArticleInfo(Integer id) {
if(id <= 0){
return null;
}
return articlesDao.getArticleInfo(id);
}
@Override
public int addArticleViewCount(Integer id) {
if(id <= 0){
return 0;
}
return articlesDao.addArticleViewCount(id);
}
@Override
public int addPraiseCount(Integer id) {
return 0;
}
@Override
public List<Articles> queryApiArticlesList(Integer type, String title, Integer page, Integer pageSize) {
return null;
}
@Override
public List<Articles> hotArticlesList() {
return articlesDao.hotArticlesList();
}
}
检查springboot启动文件BoboServiceApplication
发现没有包含ComponentScan,由于我这个项目是分模块的项目,启动文件和调用的service不在同一个包,那么可能扫描不到包,导致无法找到自动注入的类。
package com.bowen.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class BoboServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BoboServiceApplication.class, args);
}
}
解决方案
BoboServiceApplication 加入如下代码
package com.bowen.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* 加入需要扫描的包地址,防止出现找不到注入对象的问题
*/
@ComponentScan(basePackages = {"com.bowen.*"})
@SpringBootApplication
public class BoboServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BoboServiceApplication.class, args);
}
}
总结
- ComponentScan做的事情就是告诉Spring从哪里找到bean
- 如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
- 如果你有一些bean所在的包,不在main app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包