I'm confused with regards to dependency injection. What I want to achieve is to replace the GlobalSettings.onStart() call, where I initialized some static singleton objects in 2.3, with proper dependency injection of those objects.
What I am trying to do is this:
Controller -> Model (inject an object into this model)
What I have so far is a half way measure; in the controller:
private static SomeObject myStaticSingletonObject = new SomeObject();
public Promise<Result> getSomeData() {
return handleRequest(() -> new SomeDataAjaxRequest(myStaticSingletonObject));
}
public Promise<Result> handleRequest(Function0<AbstractAjaxRequest<?>> supplier) {
Promise<AbstractAjaxRequest<?>> promise = Promise.promise(supplier);
return promise.map(arg -> ok(arg.getResponse()));
}
handleRequest() is a custom method I use and isn't really related but I include it for completeness:
And in the model I just take the SomeObject as a param:
private final SomeObject someObject;
public SomeDataAjaxRequest(SomeObject someObject) {
super(null);
this.someObject = someObject;
}
In my build.sbt I have:
routesGenerator := InjectedRoutesGenerator
So basically my question is how should I be injecting SomeObject into my models and also how should I be creating my SomeObject object, I don't think I should be using new SomeObject().
Ideally I would like to use field injection for these objects as I don't want to clutter the constructor which might actually have relevant params for the model rather than just these utility classes that contain definitions of things (SomeObject basically just loads some information from the database which currently is static throughout the lifetime of the application, but that may change).
Also it is probably worth noting that I am intending to use Guice to manage DI.
I understand that I should create a Guice DI factory and have seen the docs for this but I still am unsure how to integrate this into my play app.