Package ratpack.guice

Class ConfigurableModule<T>

  • Type Parameters:
    T - the type of the config object
    All Implemented Interfaces:
    Module
    Direct Known Subclasses:
    ClientSideSessionModule, DropwizardMetricsModule, HandlebarsModule, HikariModule, HystrixModule, MarkupTemplateModule, RedisSessionModule, SessionModule, TextTemplateModule, ThymeleafModule

    public abstract class ConfigurableModule<T>
    extends AbstractModule
    Provides a standard approach for modules that require some parametrization / configuration.

    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() {}
    
         @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() {
         }
    
         @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 Detail

      • ConfigurableModule

        public ConfigurableModule()
    • 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 by createConfig(ServerConfig).

        This can be used if it's not possible to apply the configuration in the constructor.

        Parameters:
        serverConfig - the application server config
        config - 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(ratpack.server.ServerConfig)