Suppose I have these made-up classes (I know nothing about cars btw)
@Singleton
class ElectricEngine implements Engine {
}
class Vehicle {
private final Engine engine;
Vehicle(Engine engine, SteeringSystem ss, ...) {
this.engine = engine;
}
}
Normally, child classes would be built in this way
class Tesla extends Vehicle {
@Inject
Tesla(ElectricEngine engine, SteeringSystem ss) {
super(engine, ss);
}
}
What I'd like to do not have the child specify the Engine and have a class such as
class ElectricVehicle extends Vehicle {
ElectricVehicle(SteeringSystem ss) {
super(electricEngine???, ss);
}
}
class Tesla extends ElectricVehicle {
@Injected
Tesla(SteerByWireSystem ss) {
super(ss);
}
}
The question is, how can ElectricVehicle get the ElectricEngine object to pass to its parent?
I considered doing field injection but not sure if that would solve it and also not a fan of it based on this.
Any ideas?
If you want to use the Injection to handle your bean, you'll need to inject something.
An alternative, pure Java, solution to a singleton is to have the class hold it's own singleton instance:
Additional ideas based on authors comment. Reverting to Spring because it's what I know best.
I would probably go for something like this:
In general, I prefer the more controlled way of creating beans via methods rather than deferring it to the injection framework.