Idea 注解错误could not autowire no beans of...plicationEventMulticaster not initialized

could not autowire no beans of xx这个问题是经常遇到,一般加个@Compent之类的就可以解决。但是有些时候可能不那么好调试。

网上有说好多种情况,不影响程序运行的就不说了,更改报错的级别始终是旁门左道。缺包或包冲突的也容易解决。idea有个显示jar依赖关系的图。
Idea 注解错误could not autowire no beans of...plicationEventMulticaster not initialized_第1张图片

快捷键是ctr+alt+shift+u,不同版本图标可能不一样。

现在说下我autowire问题。

Idea 注解错误could not autowire no beans of...plicationEventMulticaster not initialized_第2张图片

我的UserService是从外部依赖进来的,
Idea 注解错误could not autowire no beans of...plicationEventMulticaster not initialized_第3张图片

从上图可以看到启动类跟service类不在同一目录级别,启动类启动扫描的时候扫描不到service项目下所有的注解,所以会报这个异常。

解决方案:
1、将启动类放到更高级别的包下,我这里放shirotest下就可以了。
2、在启动类下添加扫描路径@ComponentScan(basePackages = {"com.shrio.shirotest.service","com.shrio.shirotest.myshiro"})

Idea 注解错误could not autowire no beans of...plicationEventMulticaster not initialized_第4张图片

但是这时候要把所有报路径都添加上去,如果依赖项目多了会很麻烦,所以还是更改启动类的级别更合适。
当然,将上面的包扫描到更高级别也可以@ComponentScan(basePackages = {"com.shrio.shirotest"})

如果有需要的话,还是用第一种方式吧,省事。

你可能感兴趣的:(SpringBoot,其它)