Reputation: 75
I have SomeStartegy interface with two implementations:
@Primary
@Component
public class OneStrategy implements SomeStrategy {...}
@Component
public class SecondStrategy implements SomeStrategy {...}
I need one of them to be used as a default (primary) implementation and the other to override the default in some cases.
So I wrote something like this:
public class SuperClass {
@Autowired
SomeStrategy strategy;
}
public class SubClass extends SuperClass {
@Autowired
public SubClass(SecondStrategy secondStrategy) {
this.strategy = secondStrategy;
}
}
Injecting SubClass, I can see in debug that it's ctor is called and the assignment is done like I would expect.
However, somehow it ends up with an instance of OneStrategy instead.
What am I missing here? or am I doing this all wrong?
Upvotes: 1
Views: 614
Reputation: 691635
Field injection is made after constructor injection.
Use constructor injection for the superclass too, and call super(secondStrategy)
from the subclass constructor.
Upvotes: 1