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

  2. 被警告代码:

@Autowired
    private EduWorkInfoMapper eduWorkInfoMapper;
 
    @Autowired
    private IEduExportUserInfoService eduExportUserInfoService;
  1. 原因:不建议使用属性注入,其实当你鼠标滑动到 @Autowired 上面,同时按下 alt+enter 键时,就会看到下面这样的提示 xxx to constructor, 这说明编辑器建议你使用构造注入的方式进行 bean 的注入。

  2. 说明:

参考地址

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 翻译过来的)

  1. 解决方法:参考地址: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;
    }
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Jalen Chu 微信支付

微信支付

Jalen Chu 支付宝

支付宝

Jalen Chu 公众号

公众号