I am trying to inject a (Scala) controller from another (Java) controller, using javax.inject.Inject. The code looks something like this (simplified):
Scala Controller
class A @Inject()(config: Configuration) extends InjectedController with Logging {
@Trace
def getValues(): Action[AnyContent] = {
...
}
}
Java Controller
public class B extends Controller {
@Inject
Configuration configuration;
private A a;
public AuditToolsController() {
this.a = new A(configuration);
}
public run(){
a.getValues();
}
}
But I am getting a java.util.NoSuchElementException: ControllerComponents not set! Call setControllerComponents or create the instance with dependency injection. error. I can't seem to find out how to import and call setControllerComponents, it seems to be defined in the InjectedController trait in the Controller.scala class (which I can't extend in the Java class). How can I call this function to make this error go away?
I have also tried to inject the A controller directly, e.g. @Inject A a but that leads to a java.lang.NullPointerException, I'm not sure why..
My knowledge of Play, Java, and dependency injection is very rudimentary so apologies if the error is obvious, but I couldn't find any documentation or other stack overflow questions that address this issue.