Class ConfigurableModule<T>
- java.lang.Object
-
- com.google.inject.AbstractModule
-
- ratpack.guice.ConfigurableModule<T>
-
- Type Parameters:
T
- the type of the config object
- All Implemented Interfaces:
com.google.inject.Module
- Direct Known Subclasses:
ClientSideSessionModule
,DropwizardMetricsModule
,HandlebarsModule
,HikariModule
,MarkupTemplateModule
,RedisSessionModule
,SessionModule
,TextTemplateModule
,ThymeleafModule
public abstract class ConfigurableModule<T> extends com.google.inject.AbstractModule
Provides a standard approach for modules that require some parametrization / configuration.A configurable module provides a single, mutable, “config object” (type parameter
C
). TheBindingsSpec.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 namedConfig
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.jupiter.api.Assertions.*; 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) 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.jupiter.api.Assertions.*; 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) 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 Summary
Constructors Constructor Description ConfigurableModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 bycreateConfig(ServerConfig)
.void
setConfig(T config)
Sets the config object for this module.-
Methods inherited from class com.google.inject.AbstractModule
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
-
-
-
-
Method Detail
-
configure
public void configure(Action<? super T> configurer)
Registers the configuration action.This method is called by
BindingsSpec.module(Class, Action)
.- Parameters:
configurer
- the configuration action.
-
createConfig
protected T createConfig(ServerConfig serverConfig)
Creates the configuration object.This implementation reflectively creates an instance of the type denoted by type param
T
. In order for this to succeed, the following needs to be met:- The type must be public.
- Must have a public constructor that takes only a
ServerConfig
, or takes no args.
If the config object cannot be created this way, override this method.
- Parameters:
serverConfig
- the application launch config- Returns:
- a newly created config object
-
defaultConfig
protected void defaultConfig(ServerConfig serverConfig, T config)
Hook for applying any default configuration to the configuration object created bycreateConfig(ServerConfig)
.This can be used if it's not possible to apply the configuration in the constructor.
- Parameters:
serverConfig
- the application server configconfig
- the config object
-
setConfig
public void setConfig(T config)
Sets the config object for this module. This overrides the default config object that would be created otherwise.- Parameters:
config
- the config object- See Also:
createConfig(ServerConfig)
-
-