As I know, field injection
is not recommended. Should use constructor
instead.
What I\'m trying to do here is using @Autowired
in
The code that you provide won't compile. As long as in your base class you don't have the default constructor, you should call super(MyDemoService)
in child.
Inherited(sub) class:
public class Sub extends Base {
private final MySubService mySubService;
@Autowired
public Sub(MySubService mySubService, MyDemoService myDemoService){
super(myDemoService);
this.mySubService = mySubService;
}
}
Or, if MySubService
is an implementation of MyDemoService
@Autowired
public Sub(MySubService mySubService){
super(mySubService);
}
As long as your field MyDemoService myDemoService
in abstract class is protected
you can use it in subclasses.
If you have multiple implementation of MyDemoService
, than you have to use @Qualifier
as described in the answer that you have mentioned.
public Sub(@Qualifier("MySubService") MyDemoService mySubService){
super(mySubService);
}