【已解决】Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.

问题

在项目中,我们使用Spring的@Autowired注解去引入其他类时有时候阿里的编码规约插件就会提示:“Field injection is not recommended”或“Could not autowired. No beans of 'xxx' type found.”,引用类的变量名会有红色的波浪线,虽然不影响程序执行,但是强迫症看着还是难受。

解决办法

将“@Autowired”注解换为“@Resouce”注解。
更换注解后:

备注

@Resource是JSR-250规范中的注解,目前Spring已支持该规范。@Resource是先根据Bean的名称去匹配Bean,获取不到的话再根据类型去匹配;而@Autowired则是根据类型匹配,通过名称则需要Spring的@Qualifier注解配合。

posted @ 2019-11-05 21:03  🐫沙漠骆驼  阅读(7332)  评论(1)    收藏  举报