IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

1.问题

最近在用idea开发一个Maven工程,采用ssm架构,在controller中经常会出现这种错误

IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

提示我们Could not autowire. No beans of 'xxx' type found.

这个错误很奇怪,虽然我的service是通过dubbo从外部引入的,但是各种配置都正确无误,运行也没有任何问题,所以出现这种错误让我感觉摸不着头脑

2.解决

网上说的那些修改autowire检测级别的方法感觉不太cool,后面如果真的出现autowire错误反而检测不到,引起不必要的麻烦

因此我这里提出另外一种解决方案:

首先我们点击file->Project Structured

IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

点击Facets,看到右边那一堆spring没有,现在我们依次选中,然后删除,删干净,然后点击apply,保存。

现在看一下,错误是不是已经消失了呢:

IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

不过这时在我们的spring配置文件中会出现:

IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

不需要点右边那个,不然又会出现错误。如果觉得碍眼的话,可以点最左边那个齿轮,然后edit inspection profile setting

IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

这样就不会出现了。

3.补充说明

这个spring facet呢我感觉没有多大的用处,配置了之后只会多一个spring的视图,在最下面:

IntelliJ Idea中maven工程spring报Could not autowire. No beans of 'xxx' type found异常的正确解决方法

我猜想原因是因为配置了facet,spring会在每个工程检测各种配置文件的依赖性,而我们在一个工程依赖另一个工程的service或者dao时,它会检测不到,所以报异常。我觉得还有一种解决途径就是把所有工程的spring配置文件配在一个facet中,应该也不会报错了,不过我还没试过(因为觉得很麻烦哈哈哈哈哈)。