18

I have an issue with the AssistedInject. I followed the instructions on this link https://github.com/google/guice/wiki/AssistedInject but when I run my application I get an error:

ERROR [2015-04-23 14:49:34,701] com.hubspot.dropwizard.guice.GuiceBundle: Exception occurred when creating Guice Injector - exiting
! com.google.inject.CreationException: Unable to create injector, see the following errors:
!
! 1) A binding to java.lang.String annotated with @com.google.inject.assistedinject.Assisted(value=) was already configured at com.demo.migrator.service.democlient.DemoAPIFactory.create().
!   at com.demo.migrator.service.democlient.DemoAPIFactory.create(DemoAPIFactory.java:1)
!   at com.google.inject.assistedinject.FactoryProvider2.initialize(FactoryProvider2.java:577)
!   at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:335) (via modules: com.demo.migrator.MigrationModule -> com.google.inject.assistedinject.FactoryModuleBuilder$1)

Here is my module configuration :

install(new FactoryModuleBuilder()
    .implement(DemoAPI.class, DemoClient.class)
    .build(DemoAPIFactory.class));

Here is how my factory looks like:

 public interface DemoAPIFactory {
   DemoAPI create(String _apiKey, String _secretKey);
 }

The interface is declared as following:

public interface DemoAPI {
   //list of interface methods
}

And here is the implementation

 @Inject
public DemoClient(@Assisted String _apiKey, 
       @Assisted String _secretKey) {
    secretKey = _secretKey;
    apiKey = _apiKey;
    baseURL = "xxxxx";
    expirationWindow = 15;
    roundUpTime = 300;
    base64Encoder = new Base64();
    contentType = "application/json";
}

I am using dropwizard guice bundle.

Why is this error occurring?

almy
  • 193
  • 1
  • 1
  • 7
  • 3
    Hi! I know you've been around awhile but you haven't asked many questions. There isn't enough information here to answer your question; please read [ask]. Specifically, that line of your module configuration looks ok, but we need to see the **constructor for DemoClient.class** and the **DemoAPIFactory code**. Please [edit] them into your question. – durron597 Apr 23 '15 at 13:52
  • 1
    Thank you durron, I have updated all the information. – almy Apr 24 '15 at 07:50
  • Do you have `DemoApi` defined anywhere else? Such as `bind(DemoApi.class)`? Look everywhere in all your modules, you most certainly have it somewhere. – Olivier Grégoire Apr 24 '15 at 16:02

1 Answers1

53

This is a common problem, the solution is documented in the javadoc:

Making parameter types distinct

The types of the factory method's parameters must be distinct. To use multiple parameters of the same type, use a named @Assisted annotation to disambiguate the parameters. The names must be applied to the factory method's parameters:

 public interface PaymentFactory {
    Payment create(
        @Assisted("startDate") Date startDate,
        @Assisted("dueDate") Date dueDate,
        Money amount);    } 

...and to the concrete type's constructor parameters:

public class RealPayment implements Payment {
  @Inject
  public RealPayment(
     CreditService creditService,
     AuthService authService,
     @Assisted("startDate") Date startDate,
     @Assisted("dueDate") Date dueDate,
     @Assisted Money amount) {
     ...
  }    }
Jan Galinski
  • 11,768
  • 8
  • 54
  • 77