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.module(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;
import static org.junit.Assert.*;
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() {}
{@literal @}Provides
String provideString(Config config) {
return config.value;
}
}
public static void main(String... args) throws Exception {
EmbeddedApp.of(s -> s
.registry(Guice.registry(b -> b.module(StringModule.class, c -> c.value("foo"))))
.handlers(chain -> chain.get(ctx -> ctx.render(ctx.get(String.class))))
).test(httpClient -> {
assertEquals("foo", httpClient.getText());
});
}
}
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;
import static org.junit.Assert.*;
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() {
}
{@literal @}Provides
String provideString(Config config) {
return config.value;
}
}
public static void main(String... args) throws Exception {
EmbeddedApp.of(s -> s
.registry(Guice.registry(b -> b
.module(StringModule.class)
.bindInstance(new StringModule.Config().value("bar"))
))
.handlers(chain -> chain
.get(ctx -> ctx.render(ctx.get(String.class)))
)
).test(httpClient -> {
assertEquals("bar", httpClient.getText());
});
}
}
Constructor and Description |
---|
ConfigurableModule() |
Modifier and Type | Method and Description |
---|---|
void |
configure(Action<? super T> configurer)
Registers the configuration action.
|
protected T |
createConfig(ServerConfig serverConfig)
Creates the configuration object.
|
protected void |
defaultConfig(ServerConfig serverConfig,
T config)
Hook for applying any default configuration to the configuration object created by
createConfig(ServerConfig) . |
void |
setConfig(T config)
Sets the config object for this module.
|
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
public void configure(Action<? super T> configurer)
This method is called by BindingsSpec.module(Class, Action)
.
configurer
- the configuration action.protected T createConfig(ServerConfig serverConfig)
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:
ServerConfig
, or takes no args.If the config object cannot be created this way, override this method.
serverConfig
- the application launch configprotected void defaultConfig(ServerConfig serverConfig, T config)
createConfig(ServerConfig)
.
This can be used if it's not possible to apply the configuration in the constructor.
serverConfig
- the application server configconfig
- the config objectpublic void setConfig(T config)
config
- the config objectcreateConfig(ratpack.server.ServerConfig)