T - the type of the config objectpublic abstract class ConfigurableModule<T> extends AbstractModule
A configurable module provides a single, mutable, “config object” (type parameter C).
The BindingsSpec.add(Class, Action) method can be used to add the module and configure it at the same time.
It is conventional, but not required, for the config type to be a nested static class named Config of the module class.
import com.google.inject.Provides;
import ratpack.guice.ConfigurableModule;
import ratpack.guice.Guice;
import ratpack.test.embed.EmbeddedApp;
public class Example {
public static class StringModule extends ConfigurableModule<StringModule.Config> {
public static class Config {
private String value;
public void value(String value) {
this.value = value;
}
}
protected void configure() {}
@Provides
String provideString(Config config) {
return config.value;
}
}
public static void main(String... args) {
EmbeddedApp.fromHandlerFactory(launchConfig ->
Guice.builder(launchConfig)
.bindings(b ->
b.add(StringModule.class, c -> c.value("foo"))
)
.build(chain -> chain
.get(ctx ->
ctx.render(ctx.get(String.class))
)
)
).test(httpClient -> {
assert httpClient.getText().equals("foo");
});
}
}
Alternatively, the config object can be provided as a separate binding.
import com.google.inject.Provides;
import ratpack.guice.ConfigurableModule;
import ratpack.guice.Guice;
import ratpack.test.embed.EmbeddedApp;
public class Example {
public static class StringModule extends ConfigurableModule<StringModule.Config> {
public static class Config {
private String value;
public Config value(String value) {
this.value = value;
return this;
}
}
protected void configure() {
}
@Provides
String provideString(Config config) {
return config.value;
}
}
public static void main(String... args) {
EmbeddedApp.fromHandlerFactory(launchConfig ->
Guice.builder(launchConfig)
.bindings(b -> b
.add(StringModule.class)
.bindInstance(new StringModule.Config().value("bar"))
)
.build(chain -> chain
.get(ctx ->
ctx.render(ctx.get(String.class))
)
)
).test(httpClient -> {
assert httpClient.getText().equals("bar");
});
}
}
| Constructor and Description |
|---|
ConfigurableModule() |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(ratpack.func.Action<? super T> configurer)
Registers the configuration action.
|
protected T |
createConfig(ratpack.launch.LaunchConfig launchConfig)
Creates the configuration object.
|
protected void |
defaultConfig(ratpack.launch.LaunchConfig launchConfig,
T config)
Hook for applying any default configuration to the configuration object created by
createConfig(LaunchConfig). |
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBindingpublic void configure(ratpack.func.Action<? super T> configurer)
This method is called by BindingsSpec.add(Class, Action).
configurer - the configuration action.protected T createConfig(ratpack.launch.LaunchConfig launchConfig)
This implementation reflectively creates an instance of the type denoted by type param C.
In order for this to succeed, the following needs to be met:
LaunchConfig, or takes no args.If the config object cannot be created this way, override this method.
launchConfig - the application launch configprotected void defaultConfig(ratpack.launch.LaunchConfig launchConfig,
T config)
createConfig(LaunchConfig).
This can be used if it's not possible to apply the configuration in the constructor.
launchConfig - the application launch configconfig - the config object