Play 2.4 supports dependency injected controllers out of the box. I have successfully used constructor injection to provide dependencies to my controllers. However, when using action composition, fields marked with @Inject
are not injected.
Is there any way to inject dependencies into a composite action?
Example code for controller:
public class Application extends Controller {
private DomainService ds;
@Inject
public Application(DomainService ds) {
this.ds = ds;
}
@Security.Authenticated(RandomAuthenticator.class)
public Result index() {
return ok();
}
}
Example code for composite action:
public class RandomAuthenticator extends Security.Authenticator {
@Inject private RandomService rs; // This field is never injected
@Override
public String getUsername(Context context) {
float randFloat = rs.nextFloat(); // Error! rs is always null
if (randFloat > 0.1) {
return "foo";
}
return null;
}
}