idea警告:Field injection is not recommended

  • Post author:
  • Post category:其他


1. 使用@Autowire注入时警告:Field injection is not recommended

2. 被警告代码:

    @Autowired
    private EduWorkInfoMapper eduWorkInfoMapper;

    @Autowired
    private IEduExportUserInfoService eduExportUserInfoService;

3. 警告显示:


4. 原因:不建议使用属性注入,其实当你鼠标滑动到@Autowired上面,同时按下alt+enter键时,就会看到下面这样的提示


这说明编辑器建议你使用构造注入的方式进行bean的注入。

5. 说明:

参考地址


http://vojtechruzicka.com/field-dependency-injection-considered-harmful/


https://www.petrikainulainen.net/software-development/design/why-i-changed-my-mind-about-field-injection/


https://stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it


http://www.bubuko.com/infodetail-2048216.html

(从StackOverflow翻译过来的)

6. 解决方法:

参考地址:

http://blog.csdn.net/jeikerxiao/article/details/77161680

    private IWorkLoadService workLoadService;
    private IEduExportUserInfoService eduExportUserInfoService;
    private IWorkLoadJobsService workLoadJobsService;

    @Autowired
    public WorkLoadController( IWorkLoadService workLoadService
            ,IEduExportUserInfoService eduExportUserInfoService
            ,IWorkLoadJobsService workLoadJobsService ){
        this.workLoadService = workLoadService;
        this.eduExportUserInfoService = eduExportUserInfoService;
        this.workLoadJobsService = workLoadJobsService;
    }



版权声明:本文为baidu_30809315原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。