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.
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(Action<? super T> configurer)
Registers the configuration action.
|
protected T |
createConfig(LaunchConfig launchConfig)
Creates the configuration object.
|
protected void |
defaultConfig(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, requireBinding
public void configure(Action<? super T> configurer)
This method is called by BindingsSpec.add(Class, Action)
.
configurer
- the configuration action.protected T createConfig(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(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