Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.
Posted csh24
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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注解配合。
以上是关于Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.的主要内容,如果未能解决你的问题,请参考以下文章
IDEA警告:Field injection is not recommended
@Autowired注解警告Field injection is not recommended
Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.
JAVA错误提示:The operation is not applicable to the current selection.Select a field which is not declar
Django admin prepopulate field not error "d.Keyup is not a function"
CloudKit Console 页面中查询数据出现 Field ‘recordName‘ is not marked queryable 错误的解决