use @autowired in abstract base class

后端 未结 5 1604
失恋的感觉
失恋的感觉 2021-02-05 13:12

As I know, field injection is not recommended. Should use constructor instead.

What I\'m trying to do here is using @Autowired in

5条回答
  •  心在旅途
    2021-02-05 14:05

    Don't use field injection, use constructor injection calling back to super constructors as appropriate.

    Constructor injection ensures your object is properly populated before instantiation, setter injection does not and makes code more bug prone (not another nullpointer bug....)

    If you are concerned about the extra code you have to write then use Project Lombok to let Lombok generate the constructor code for you as described here Why field injection is evil

    By the way as of Spring 4 if you only have a single constructor in your class you don't need @Autowired on the constructor.

提交回复
热议问题