idea警告:Field injection is not recommended

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





2. 被警告代码:

    @Autowired
    private EduWorkInfoMapper eduWorkInfoMapper;

    @Autowired
    private IEduExportUserInfoService eduExportUserInfoService;





3. 警告显示:

idea警告:Field injection is not recommended_第1张图片





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

idea警告:Field injection is not recommended_第2张图片

这说明编辑器建议你使用构造注入的方式进行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;
    }







你可能感兴趣的:(IDE)