1

I have a Module that provides a JDBI DBI instance like this:

@Provides
@Singleton
DBI dbi(DataSource dataSource) { return new DBI(dataSource); }

In another module I want to call some initialization methods on that DBI instance (configuring support for particular data types). It's not appropriate logic to put in the JDBI module itself as it's application specific not general to any application using JDBI. Is there a hook for me to do that kind of "extra" configuration?

I tried using the bindListener method but it seems like that's not invoked for objects provided in this way.

durron597
  • 31,968
  • 17
  • 99
  • 158
Rob Fletcher
  • 8,317
  • 4
  • 31
  • 40
  • `bindListener` is not notified about `@Provides` created objects. This doesn't answer the question but can you bind DBI in your `configure()` method? Then your type listener would be notified and could perform the extra configuration... – condit Jun 19 '14 at 16:12
  • By the way, this is the second Guice question in a row you've asked without tagging [tag:java]. If you don't tag Java, you're going to get a lot fewer views; even though dependency injection is not a Java specific idea, **Guice** is, so you should tag it that way. – durron597 Jun 23 '14 at 13:25
  • @durron597 seemed like an overly broad tag to me – Rob Fletcher Jun 24 '14 at 10:53

2 Answers2

9

The Guice Injections documentation describes how to invoke an instance method by annotating the method with @Inject.

It doesn't mention that the instance can be a Guice module. As such, you can do this:

class MyConfigurationModule extends AbstractModule {
  @Override
  protected void configure() {
    requestInjection(this);
  }

  @Inject
  void configureDbi(DBI dbi) {
    // Do whatever configuration.
  }  
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Thanks, that's a great way to do it. I'd figured I could override the DBI from another module, but this is much much neater. – Rob Fletcher Jun 30 '14 at 12:06
0

Guice is designed to have many different modules for different purposes. This allows you to swap settings and other injections while leaving others the same.

So you could do something like the following:

  • Create an annotation for your application to use with your DBI dependency injection:

Example:

import com.google.inject.BindingAnnotation;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;

@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
public @interface ApplicationDBI {}
  • Create a second module for DBI specific to your application.

Example:

@Provides @ApplicationDBI
// Choose the appropriate scope here
protected DBI provideConfiguredDBI(DBI baseDBI) {
    // configure the baseDBI here
    return baseDBI;
}
  • Annotate the application injection with your new annotation

Example:

public class DatabaseWrapper {
    private final DBI dbi;
    @Inject
    DatabaseWrapper(@ApplicationDBI DBI dbi) {
        this.dbi = dbi;
    }
}

Then in your main method:

Injector inj = Guice.createInjector(new DatabaseModule(),
                 new ApplicationDBIModule(),
                 ...);
durron597
  • 31,968
  • 17
  • 99
  • 158