Class AbstractJdbiDefinitionModule

java.lang.Object
com.google.inject.PrivateModule
org.jdbi.v3.guice.AbstractJdbiDefinitionModule
All Implemented Interfaces:
Module, JdbiBinder

public abstract class AbstractJdbiDefinitionModule extends PrivateModule implements JdbiBinder
Base module class to define Jdbi instances. This is a PrivateModule which will by default only expose the constructed Jdbi object as a binding. Everything configured within this module is private to the module and not exposed. This allows installation of multiple instances of modules that extend the AbstractJdbiDefinitionModule base class.

Each module requires a DataSource bound using the same annotation or annotation class as used on the module constructor. If this data source is not present, the injection process will fail.

Successful binding will expose a Jdbi binding annotated with the same annotation or annotation class as used on the module constructor. Additional elements defined in this module can be exposed using the exposeBinding(Class) is called within the configureJdbi() method.

  • Constructor Details

    • AbstractJdbiDefinitionModule

      public AbstractJdbiDefinitionModule(Annotation annotation)
      Create a Jdbi definition module.

      A Jdbi definition module that uses this constructor will use the default element configuration from AbstractJdbiConfigurationModules that do not use any custom annotation.

      Parameters:
      annotation - The resulting @{link Jdbi} instance will be exposed using this annotation.
    • AbstractJdbiDefinitionModule

      public AbstractJdbiDefinitionModule(Annotation annotation, Class<? extends Annotation> globalAnnotationClass)
      Create a Jdbi definition module with a custom annotation for element configuration.

      A Jdbi definition module that uses this constructor will use the element configuration from AbstractJdbiConfigurationModules that use the same annotation as this constructor.

      Parameters:
      annotation - The resulting @{link Jdbi} instance will be exposed using this annotation.
      globalAnnotationClass - Custom annotation class used to look up global settings for this Jdbi instance.
    • AbstractJdbiDefinitionModule

      public AbstractJdbiDefinitionModule(Class<? extends Annotation> annotationClass)
      Create a Jdbi definition module.

      A Jdbi definition module that uses this constructor will use the default element configuration from AbstractJdbiConfigurationModules that do not use any custom annotation.

      Parameters:
      annotationClass - The resulting @{link Jdbi} instance will be exposed using this annotation class.
    • AbstractJdbiDefinitionModule

      public AbstractJdbiDefinitionModule(Class<? extends Annotation> annotationClass, Class<? extends Annotation> globalAnnotationClass)
      Create a Jdbi definition module with a custom annotation for element configuration.

      A Jdbi definition module that uses this constructor will use the element configuration from AbstractJdbiConfigurationModules that use the same annotation as this constructor.

      Parameters:
      annotationClass - The resulting @{link Jdbi} instance will be exposed using this annotation.
      globalAnnotationClass - Custom annotation class used to look up global settings for this Jdbi instance.
  • Method Details

    • configureJdbi

      public abstract void configureJdbi()
      Override this method to configure all aspects of a Jdbi instance.
       @Override
       protected void configureJdbi() {
         bindPlugin().toInstance(...);
         bindTransformer().to(...);
         bindRowMapper().to(...);
       }
       
    • jdbiBinder

      public final JdbiBinder jdbiBinder()
      Provides access to the JdbiBinder instance.

      Only valid when called from configureJdbi().

      Specified by:
      jdbiBinder in interface JdbiBinder
    • importBinding

      public final <T> void importBinding(Class<T> clazz)
      Pulls an existing outside binding into the module scope. An outside binding uses the same annotation or annotation class as the module.
    • importBinding

      public final <T> void importBinding(TypeLiteral<T> type)
      Pulls an existing outside binding into the module scope. An outside binding uses the same annotation or annotation class as the module.
    • importBindingLoosely

      public final <T> void importBindingLoosely(Class<T> clazz, @Nullable T defaultValue)
      Pulls an existing outside binding loosely into the module scope. An outside binding uses the same annotation or annotation class as the module. Unlike the importBinding(Class) method, this method will use the provided default value if the requested binding can not be found.

      As the default value can be null, the resulting binding can also be null. It is therefore necessary to annotate all injection points with Nullable and also check for potential null values to be injected.

      Parameters:
      clazz - Class object describing the binding to pull into module scope.
      defaultValue - The default value to use if the binding can not be found. Can be null.
    • importBindingLoosely

      public final <T> void importBindingLoosely(TypeLiteral<T> type, @Nullable T defaultValue)
      Pulls an existing outside binding loosely into the module scope. An outside binding uses the same annotation or annotation class as the module. Unlike the importBinding(TypeLiteral) method, this method will use the provided default value if the requested binding can not be found.

      As the default value can be null, the resulting binding can also be null. It is therefore necessary to annotate all injection points with Nullable and also check for potential null values to be injected.

      Parameters:
      type - A TypeLiteral describing the binding to pull into module scope.
      defaultValue - The default value to use if the binding can not be found. Can be null.
    • createKey

      public final <T> Key<T> createKey(Class<T> clazz)
      Creates a Key object for a class that uses the annotation or annotation class used to construct this module.
    • createKey

      public final <T> Key<T> createKey(TypeLiteral<T> type)
      Creates a Key object for a TypeLiteral that uses the annotation or annotation class used to construct this module.
    • exposeBinding

      public final <T> void exposeBinding(Class<T> clazz)
      Exposes a binding that is in module scope (without annotations) and binds it using either the annotation or annotation class and then exposes it outside the module.
    • exposeBinding

      public final <T> void exposeBinding(TypeLiteral<T> type)
      Exposes a binding that is in module scope (without annotations) and binds it using either the annotation or annotation class and then exposes it outside the module.