IDEA警告:Field injection is not recommended

Posted 谷哥的小弟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEA警告:Field injection is not recommended相关的知识,希望对你有一定的参考价值。


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

背景描述

在IDEA编写Spring时发现一个警告:Field injection is not recommended

@RestController
@RequestMapping("MyController")
public class MyController {
    @Autowired
    private ArticleMapper articleMapper;
}

原因分析

在新版Spring中不再推荐使用字段注入,建议修改为基于构造函数的依赖注入。

@RestController
@RequestMapping("MyController")
public class MyController {
    private ArticleMapper articleMapper;
    @Autowired
    public MyController(ArticleMapper articleMapper) {
        this.articleMapper = articleMapper;
    }
}

以上是关于IDEA警告:Field injection is not recommended的主要内容,如果未能解决你的问题,请参考以下文章

关于@AutoWired依赖注入时有警告----Field injection is not recommended

IntelliJ IDEA:Field injection is not recommended

Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.

IDEA警告:SQL dialect is not configured

IDEA警告:SQL dialect is not configured

IDEA 程序出现 Class 'xxx' is never used 警告下划线如何消除