Could not autowire. No beans of 'xxxx' type found的错误提示(项目运行正常)

楼主使用idea编译器,经常遇到 Could not autowire. 的报错提示。
···
@Service
public class CommentService {
@Autowired
CommentDAO commentDAO;

@Autowired
SensitiveService sensitiveService;

public List<Comment> getCommentsByEntity(int entityId, int entityType) {
    return commentDAO.selectCommentByEntity(entityId, entityType);
}

···
上述代码 commentDAO 就会遇到这样的问题,google了一下才发现是idea搞得鬼,在idea中会自动查询对应bean的xml文件,如果查询未果就会报这样的错误
解决方法:
第一种 :可以再idea中点击 File -> settings -> inspections -> spring 将右侧的severity的值设置为"warning"即可(一劳永逸)。

第二种解决方法 : @Autowired 换成
@Autowired(required = false) 即可 ps: 使用 @Autowired(required = false),这等于告诉 Spring:在找不到匹配 Bean 时也不报错

第三种解决办法 ;
在Dao接口上加上了@Repository注解。(ps:如果使用ibatis插件生成文件这种方法可能会比较麻烦)

原文地址:https://www.cnblogs.com/shuoli/p/7376362.html