public interface BindingsSpec
This type is used when bootstrapping a Guice based application to add modules
and bindings.
It is recommended to become familiar with Guice bindings, scopes and other concerns before using Guice with Ratpack.
import ratpack.guice.*; import ratpack.func.Action; import com.google.inject.AbstractModule; class MyService { private final String value; public MyService(String value) { this.value = value; } } class MyModule extends AbstractModule { public String serviceValue; protected void configure() { bind(MyService.class).toInstance(new MyService(serviceValue)); } } class ModuleAction implements Action<BindingsSpec> { public void execute(BindingsSpec bindings) { // MyModule has been added by some other action that executed against this registry… bindings.config(MyModule.class).serviceValue = "foo"; } }
The order in which modules are added is significant. Subsequent modules can override the bindings of previous modules. This is a very useful technique for augmenting/customising the functionality provided by modules. Many modules provide extensive bindings to facilitate such overriding.
Bindings added via the bind()
and provider()
methods always have the highest precedence, regardless of order.
That is, non module bindings can always override module bindings regardless of whether the module is added before or after the non module binding.
Added modules can implement the HandlerDecoratingModule
interface to facilitate adding handlers implicitly to the handler chain.
Modifier and Type | Method and Description |
---|---|
void |
add(Iterable<? extends Module> modules)
Adds the bindings from the given modules.
|
void |
add(Module... modules)
Adds the bindings from the given modules.
|
void |
bind(Class<?> type)
Add a binding for the given type.
|
<T> void |
bind(Class<? super T> publicType,
T instance)
Add a binding for the given public type, to the given implementing instance.
|
<T> void |
bind(Class<T> publicType,
Class<? extends T> implType)
Add a binding for the given public type, to the given implementation type.
|
<T> void |
bind(T instance)
Add a binding for the given object to its concrete type.
|
<T extends Module> |
config(Class<T> moduleClass)
Retrieves the module that has been added with the given type for configuration.
|
LaunchConfig |
getLaunchConfig()
The launch config for the application.
|
void |
init(Action<Injector> action)
Registers an action to operate on the injector when it has been finalized.
|
void |
init(Class<? extends Runnable> clazz)
Registers a runnable to instantiated via dependency injection when the injector is created from this module registry.
|
<T> void |
provider(Class<T> publicType,
Class<? extends javax.inject.Provider<? extends T>> providerType)
Add a binding for the given public type, to the given provider type.
|
LaunchConfig getLaunchConfig()
void add(Module... modules)
modules
- modules whose bindings should be addedvoid add(Iterable<? extends Module> modules)
modules
- modules whose bindings should be added<T extends Module> T config(Class<T> moduleClass) throws NoSuchModuleException
This can be used to configure modules that have already been added by some other mechanism.
T
- the type of the module to retrievemoduleClass
- the type of the module to retrieveNoSuchModuleException
- if no module has been added with the given typevoid bind(Class<?> type)
type
- the type to add a binding for<T> void bind(Class<T> publicType, Class<? extends T> implType)
T
- the public type of the bindingpublicType
- the public type of the bindingimplType
- the class implementing the public type<T> void bind(Class<? super T> publicType, T instance)
T
- the public type of the bindingpublicType
- the public type of the bindinginstance
- the instance that implements the public type<T> void bind(T instance)
T
- the type of the bindinginstance
- the instance to bind<T> void provider(Class<T> publicType, Class<? extends javax.inject.Provider<? extends T>> providerType)
T
- The public type of the objectpublicType
- the public type of the objectproviderType
- the type of the provider for the objectvoid init(Action<Injector> action)
This can be used to do post processing of registered objects or application initialisation.
action
- the action to execute against the constructed injectorvoid init(Class<? extends Runnable> clazz)
This facilitates writing a Runnable
implementation that uses constructor injection to get hold of what it needs to for the initialization.
clazz
- the class of the runnable to execute as an init action