IDEA 使用@Autowired提示Field injection is not recommended
摘要:IDEA 使用@Autowired提示Field injection is not recommended
问题的解决办法。
在使用@Autowired注解进行bean注入,完成自动装配的工作时,IDEA经常会警告Field injection is not recommended
。点击查看警告详细:Spring Team recommends : “Always use constuctor based dependency injection in your beans ,Always use assertions for mandatory dependencies”。这句话的意思是Spring团队建议:“在对bean进行依赖注入时,请始终使用构造器注入方式,始终对强制依赖项使用断言”。
但是构造器注入方式就是因为代码不够简洁才被我们弃之不用的,而且用属性注入时,代码位一直运行正常。所以,我们可以忽略这个warning黄牌警告,其解决办法有如下两个:
将@Autowired注解替换为@Resource注解
注解@Resource是先根据Bean的名称去匹配Bean,若获取不到,则根据类型去匹配。而注解@Autowired则是根据类型匹配,若想通过名称匹配,则需要借助Spring的@Qualifier注解。
不检查属性注入
IntelliJ IDEA 版本2021.3.1 (Ultimate Edition)。修改IDEA中Settings或者Performances里面,对属性注入的检查,依次找到:Performances->editor->code style->inspection-> Spring | Spring Core | Code | Non recommended 'field' injections
,去掉钩即可,如下图所示:
IDEA 使用@Autowired提示Field injection is not recommended的更多相关文章
- @Autowired 警告 Field injection is not recommended Spring @Autowired注入
问题: 一. 在IDEA升级2017版后,发现以前使用的 @Autowired 出现了个警告 Field injection is not recommended. @Autowired的三种使用方式 ...
- IntelliJ IDEA:Field injection is not recommended
使用IntelliJ IDEA进行开发的时候,code analyze的时候会出现提示“Field injection is not recommended”. stackoverflow上有篇回答: ...
- 【已解决】Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.
目录 问题 解决办法 备注 问题 在项目中,我们使用Spring的@Autowired注解去引入其他类时有时候阿里的编码规约插件就会提示:"Field injection is not re ...
- @Autowired注解警告Field injection is not recommended
在使用spring框架中的依赖注入注解@Autowired时,idea报了一个警告 大部分被警告的代码都是不严谨的地方,所以我深入了解了一下. 被警告的代码如下: @Autowired UserDao ...
- TClientDataSet 提交时提示 Field value Required 但是未提示具体哪个字段。
TClientDataSet 提交时提示 Field value Required 但是未提示具体哪个字段. 这个错误特别麻烦,要使用 midas 控件时,虽然很方便.但是出错了根本找不到原因,特别是 ...
- 导入mysql数据的时候提示Field * doesn't have a default value解决方法
项目使用django+mysql 在linux中使用的是mysql5.7,导入数据提示:Field * doesn't have a default value 想要解决问题就需要知道在mysql5. ...
- SpringBoot 启动的时候提示 Field *** in *** required a bean named 'entityManagerFactory' that could not be found.
错误截图 后面发现原来和入口类代码有关. //@SpringBootApplication(scanBasePackages = {"org.jzc.odata.cboard",& ...
- idea Autowired 提示红色的解决方式
- 利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired
大家平时使用spring依赖注入,都是怎么写的? @Servicepublic class OrderService {@Autowiredprivate UserService userServic ...
- 解决@Autowired警告
在使用spring框架中的依赖注入注解@Autowired时,idea报了一个警告 被警告的代码如下: @Autowired UserDao userDao; 警告提示信息:Field injecti ...
随机推荐
- C# 一般处理程序ashx用于验证码
1.用VS2019建立一个web应用程序,选mvc模板 2.选中项目邮件新建文件夹Webservice,然后添加一般处理程序Verify.ashx然后右键打开改写如下 1 public class V ...
- MySQL时间溢出原理、实战影响与全面解决方案
一.问题背景与现象复现 操作场景: 本文将手把手带您了解mysql时间溢出原理.实战影响与全面解决方案,所有代码均通过dblens for mysql数据库工具验证,推荐使用该工具进行可视化数据库管理 ...
- 鸿蒙开发 HarmonyOS DevEco Studio 常用快捷键
前言 做 HarmonyOS 鸿蒙开发离不开 DevEco Studio 开发工具, DevEco Studio 是基于 IntelliJ IDEA Community 开源版本打造,所以默认的快捷键 ...
- centos安装JDK11
(一)建立目录 mkdir /home/jdk11 (二) 上传JDK的离线liunx包 cd /home/jdk11 示例:jdk-11.0.19_linux-x64_bin.tar.gz (三) ...
- K8S 问题排查: cgroup 内存泄露问题
Posted on 2019年12月6日Leave a comment Contents [hide] 1 前言 2 现象 3 原因 4 解决方案 4.1 方案一 4.2 方案二 4.3 方案三 5 ...
- [每日算法 - 华为机试] leetcode20 :有效的括号 「栈」
入口 力扣https://leetcode.cn/problems/valid-parentheses/submissions/ 题目描述 给定一个只包括 '(',')','{','}','[','] ...
- 【SpringCloud】SpringCloud Alibaba Seata处理分布式事务
SpringCloud Alibaba Seata处理分布式事务 分布式事务问题 分布式前 单机库存没这个问题 O(∩_∩)O 从1:1->1:N->N:N 分布式之后 单体应用被拆分成微 ...
- Linux系统中的目录和文件夹的区别
Linux系统中的目录和文件夹的区别 目录 Linux系统中的目录和文件夹的区别 一.概念与术语背景 1.目录(Directory) 2.文件夹(Folder) 二.技术实现差异 1.存储内容 2.权 ...
- HTML5 定时通知
通过setInterval()和Notification来实现定时通知功能. demo <script> window.onload = function () { //每10秒弹出一个桌 ...
- nodejs中使用websockets
websockets介绍 websockets这个新协议为客户端提供了一个更快.更有效的通信线路.像HTTP一样,websockets运行在TCP连接之上,但是它们更快,因为我们不必每次都打开一个新的 ...