新玩法-使用AllArgsConstructor+filal代替autowired

 

 和下面的代码一样:

 

Springboot官方建议使用final来修饰成员变量,然后通过构造方法来进行注入原因:final修饰的成员变量是不能够被修改的,反射那就没办法了

 还有一种写法:

@RequiredArgsConstructor(onConstructor =@_(@Autowired))
写在类上可以代替@AutoWired注解,需要注意的是在注入时需要用final定义,或者使用@notnull注解
posted @ 2020-11-24 15:30  三号小玩家  阅读(2680)  评论(0编辑  收藏  举报
Title
三号小玩家的 Mail: 17612457115@163.com, 联系QQ: 1359720840 微信: QQ1359720840