美文网首页
IntelliJ Idea解决Could not autowir

IntelliJ Idea解决Could not autowir

作者: 墨色尘埃 | 来源:发表于2017-11-08 16:26 被阅读0次
360截图16310401528889.png

1.问题描述
在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示。但程序的编译和运行都是没有问题的,这个错误提示并不会产生影响。

  1. 原因
    原因可能有两个,第一个是IntellijIDEA本身工具的问题。第二个便是我们导入@Service包的时候导入包错误造成的。第一种原因,spring auto scan配置,在编辑情况下,无法找不到对应的bean,于是提示找不到对应bean的错误。
  2. 解决方案
    针对第一种原因,解决办法是:降低Autowired检测的级别,将Severity的级别由之前的error改成warning或其它可以忽略的级别。


    image

    针对第二种原因,解决方案当然是导入正确的包。首先我们来看下最容易导入的错误包,如下所示:

import com.alibaba.dubbo.config.annotation.Service; //错误包
import org.springframework.stereotype.Service;  //正确的包应该是这个,切记切记啊!!!!

还有一种解决方法,在dao接口上添加@Service或者@Repository或者@Component注解(在LayerMapper类上添加@Service或者@Repository或者@Component),其实相当于第二种

@Service
public interface LayerMapper {

    /*查询图层列表*/
    List<LayerInfo> selectLayerList();

    /*传入整条sql语句查询查询图层所有点*/
    List<LinkedHashMap<String, Object>> selectLayerPointList(String sql);

    List<LinkedHashMap<String, Object>> superManagerSelect(String sql);
}

相关文章

网友评论

      本文标题:IntelliJ Idea解决Could not autowir

      本文链接:https://www.haomeiwen.com/subject/krrymxtx.html