java - How to use field injection with action composition? -
play 2.4 supports dependency injected controllers out of box. have used constructor injection provide dependencies controllers. however, when using action composition, fields marked @inject
not injected.
is there way inject dependencies composite action?
example code 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 composite action:
public class randomauthenticator extends security.authenticator { @inject private randomservice rs; // field never injected @override public string getusername(context context) { float randfloat = rs.nextfloat(); // error! rs null if (randfloat > 0.1) { return "foo"; } return null; } }
instead of field injection try using class' constructor inject guice:
public class randomauthenticator extends security.authenticator { private randomservice rs; @inject randomauthenticator(randomservice randomservice) { this.rs = randomservice; } @override public string getusername(context context) { float randfloat = rs.nextfloat(); // error! rs null if (randfloat > 0.1) { return "foo"; } return null; } }
Comments
Post a Comment