HubSpot / dropwizard-guice

Adds support for Guice to Dropwizard
Apache License 2.0
266 stars 95 forks source link

Binding annotations on providers don't seem to work #103

Open davidbyttow opened 7 years ago

davidbyttow commented 7 years ago

Perhaps I'm missing something, but I noticed binding annotations (including @Named) don't work on providers. For example:

public class TestModule extends AbstractModule {

  @Override protected void configure() {}

  @Provides
  @Named("instance")
  TestObject instance() {
    System.out.println("instance");
    return new TestObject();
  }

  @Provides
  @Singleton
  @Named("singleton")
  TestObject singleton() {
    System.out.println("singleton");
    return new TestObject();
  }
}
public classTestResource {

  private final Provider<TestObject> singletonProvider;
  private final Provider<TestObject> instanceProvider;

  @Inject GuiceTestResource(
      @Named("singleton") Provider<TestObject> singletonProvider,
      @Named("instance") Provider<TestObject> instanceProvider) {
    this.singletonProvider = singletonProvider;
    this.instanceProvider = instanceProvider;
  }

  @GET void get() {
    singletonProvider.get();
   instanceProvider.get();
  }
}

// Output:
// instance
// instance

Both providers use the default non-scoped object provider. However, if I make these different types and remove the binding annotations (e..g, TestObject1, TestObject2), then the scopes are honored it works.

Binding annotations should apply to Providers.

Known issue or am I missing something?

snagle commented 7 years ago

Im facing a very similar issue. For me I have 2 implementations of the same interface - but I am getting the wrong implementation injected even though I am using the right name

No one has commented on this for 3+ months so Im a little discouraged

My providers are like this

  @Provides
  @Named("type a")
  public RestUtility providesTypeARestUtility(TypeAConfiguration config) {
    return new RestUtility(config);
  }

  @Provides
  @Named("type b")
  public RestUtility providesTypeBRestUtility(TypeBConfiguration config) {
    return new RestUtility(config);
  }

Then in my resource I want a type a injected, but when debugging I can see I got a type b!

@Inject
public ForwardingResource(@Named("type a")RestUtility restUtil) {
  this.restUtil = restUtil;
}