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) { it.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 |
---|---|
BindingsSpec |
add(Class<? extends Module> moduleClass) |
<C,T extends ConfigurableModule<C>> |
add(Class<T> moduleClass,
Action<? super C> configuration) |
BindingsSpec |
add(Module modules)
Adds the bindings from the given modules.
|
BindingsSpec |
bind(Class<?> type)
Add a binding for the given type.
|
<T> BindingsSpec |
bind(Class<T> publicType,
Class<? extends T> implType)
Add a binding for the given public type, to the given implementation type.
|
BindingsSpec |
binder(Action<? super Binder> action)
Adds bindings by directly configuring a
Binder . |
<T> BindingsSpec |
bindInstance(Class<? super T> publicType,
T instance)
Add a binding for the given public type, to the given implementing instance.
|
<T> BindingsSpec |
bindInstance(T instance)
Add a binding for the given object to its concrete type.
|
<T extends Module> |
config(Class<T> moduleClass,
Consumer<? super T> configurer)
Retrieves the module that has been added with the given type for configuration.
|
LaunchConfig |
getLaunchConfig()
The launch config for the application.
|
BindingsSpec |
init(Action<? super Injector> action)
Registers an action to operate on the injector when it has been finalized.
|
BindingsSpec |
init(Class<? extends Runnable> clazz)
Registers a runnable to instantiated via dependency injection when the injector is created from this module registry.
|
<T> BindingsSpec |
provider(Class<T> publicType,
Provider<? extends T> provider)
Add a binding for the given public type, to the given provider.
|
<T> BindingsSpec |
providerType(Class<T> publicType,
Class<? extends Provider<? extends T>> providerType)
Add a binding for the given public type, to the given provider type.
|
LaunchConfig getLaunchConfig()
BindingsSpec add(Module modules)
modules
- modules whose bindings should be addedBindingsSpec add(Class<? extends Module> moduleClass)
<C,T extends ConfigurableModule<C>> BindingsSpec add(Class<T> moduleClass, Action<? super C> configuration)
<T extends Module> BindingsSpec config(Class<T> moduleClass, Consumer<? super T> configurer) 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 retrieveconfigurer
- the configurer of the moduleNoSuchModuleException
- if no module has been added with the given typeBindingsSpec binder(Action<? super Binder> action)
Binder
.action
- the binder configurationBindingsSpec bind(Class<?> type)
type
- the type to add a binding for<T> BindingsSpec 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> BindingsSpec bindInstance(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> BindingsSpec bindInstance(T instance)
T
- the type of the bindinginstance
- the instance to bind<T> BindingsSpec provider(Class<T> publicType, Provider<? extends T> provider)
T
- The public type of the objectpublicType
- the public type of the objectprovider
- the provider for the object<T> BindingsSpec providerType(Class<T> publicType, Class<? extends Provider<? extends T>> providerType)
T
- The public type of the objectpublicType
- the public type of the objectproviderType
- the type of the provider for the objectBindingsSpec init(Action<? super Injector> action)
This can be used to do post processing of registered objects or application initialisation.
action
- the action to execute against the constructed injectorBindingsSpec 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