I have a question regarding dependency injection.
I have a class, say:
class MyClass {
MyServiceAPI serviceAPI;
public setSession(Session session) {
serviceAPI = new MyServiceAPI(session);
}
....
}
A session is created by another class that loads it from the database at run time.
class MyAnotherClass {
Myclass myClass;
@Inject
MyAnotherClass(MyClass myClass) {
this.myClass = myClass;
}
public someFunction() {
String key = <read from database>;
Session session = new Session(key);
myClass.setSession();
......
}
}
As he described, an instance of MyClass is injected into MyAnotherClass, and a serviceAPI instance in MyClass is created using the new one. If I want to add the ServiceAPI to MyClass, maybe I will create it in the class where the session is created, and using the installer, set it to MyClass.
But the question is, I don't want MyAnotherClass to create MyServiceAPI and inject it into MyClass. I want MyAnotherClass not to expect MyServiceAPI to exist.
Is there a way to do this with a depdendency injector like Guice or Spring? Or just Java?
Many thanks.