使用 @Autowire 注入时警告:Field injection is not recommended
被警告代码:
@Autowired | |
private EduWorkInfoMapper eduWorkInfoMapper; | |
@Autowired | |
private IEduExportUserInfoService eduExportUserInfoService; |
原因:不建议使用属性注入,其实当你鼠标滑动到 @Autowired 上面,同时按下 alt+enter 键时,就会看到下面这样的提示 xxx to constructor, 这说明编辑器建议你使用构造注入的方式进行 bean 的注入。
说明:
参考地址
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 翻译过来的)
- 解决方法:参考地址: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; | |
} |