Class InjectionHandler
- java.lang.Object
-
- ratpack.core.handling.InjectionHandler
-
- All Implemented Interfaces:
Handler
public abstract class InjectionHandler extends Object implements Handler
A super class that removes the boiler plate of retrieving objects from the context registry by injecting them based on a method signature.Subclasses must implement exactly one method named
"handle"
that accepts aContext
as the first parameter, and at least one other parameter of any type.The
handle(Context)
method of this class will delegate to the subclass handle method, supplying values for each parameter by retrieving objects from the context and request (which are registries). The context takes precedence over the request. That is, if the context provides a value for the requested type it will be used regardless of whether the request also provides this type.The following two handlers are functionally equivalent:
import ratpack.core.handling.Context; import ratpack.core.handling.Handler; import ratpack.core.handling.InjectionHandler; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { static class Thing { public final String name; public Thing(String name) { this.name = name; } } static class VerboseHandler implements Handler { public void handle(Context context) { Thing thing = context.get(Thing.class); context.render(thing.name); } } static class SuccinctHandler extends InjectionHandler { public void handle(Context context, Thing thing) { context.render(thing.name); } } public static void main(String... args) throws Exception { EmbeddedApp.fromHandlers(chain -> chain .register(r -> r.add(new Thing("foo"))) .get("verbose", new VerboseHandler()) .get("succinct", new SuccinctHandler()) ).test(httpClient -> { assertEquals("foo", httpClient.getText("verbose")); assertEquals("foo", httpClient.getText("succinct")); }); } }
If the parameters cannot be satisfied, a
NotInRegistryException
will be thrown. TheOptional
type can be used to inject registry entries that may not exist.import ratpack.core.handling.Context; import ratpack.core.handling.InjectionHandler; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Optional; public class Example { static class OptionalInjectingHandler extends InjectionHandler { public void handle(Context context, Optional<String> string, Optional<Integer> integer) { context.render(string.orElse("missing") + ":" + integer.orElse(0)); } } public static void main(String... args) throws Exception { EmbeddedApp.fromHandlers(chain -> chain .register(r -> r.add("foo")) // no Integer in registry .get(new OptionalInjectingHandler()) ).test(httpClient -> { assertEquals("foo:0", httpClient.getText()); }); } }
If there is no suitable
handle(Context, ...)
method, aInjectionHandler.NoSuitableHandleMethodException
will be thrown at construction time.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InjectionHandler.NoSuitableHandleMethodException
Exception thrown if the subclass doesn't provide a valid handle method.
-
Constructor Summary
Constructors Modifier Constructor Description protected
InjectionHandler()
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
handle(Context context)
Invokes the custom "handle" method, extracting necessary parameters from the context to satisfy the call.
-
-
-
Constructor Detail
-
InjectionHandler
protected InjectionHandler() throws InjectionHandler.NoSuitableHandleMethodException
Constructor.- Throws:
InjectionHandler.NoSuitableHandleMethodException
- if this class doesn't provide a suitable handle method.
-
-