Annotation Interface Inject


@Retention(RUNTIME) @Target(FIELD) public @interface Inject
A service this handler needs, filled in once when the handler is bound.

 public final class ListUsers extends RequestHandler {
     @Inject private UserService users;

     @Override public Object handle(Request req, Response res) { return users.findAll(); }
 }
 

This is how a handler takes a service. onInit stays for what has to be computed at boot, or for a service that may not be there (find).

What it does, exactly

  • Filled inside bind, before onInit, once per handler instance when the route is registered. Never per request: the request path reads a field.
  • Every annotated field of the handler and of its bases up to RequestHandler is filled, private included.
  • The service is looked up by the field's declared type, exactly — not a supertype, not a generic parameter. A type nothing provides fails the boot, naming the field.
  • A static field is refused: it would be shared by every handler. A final one is refused too: it is written after construction, and a reader may have folded it.