2015-04-23 7 views
10

Ho un problema con AssistedInject. Ho seguito le istruzioni su questo link https://github.com/google/guice/wiki/AssistedInject ma quando faccio funzionare la mia domanda ho un errore:Guice assistedinject già configurato

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) 

Qui è la mia configurazione del modulo:

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

Ecco come la mia fabbrica si presenta come:

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

L'interfaccia è dichiarata come segue:

public interface DemoAPI { 
    //list of interface methods 
} 

E qui è l'implementazione

@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"; 
} 

Sto usando fascio dropwizard Guice.

Perché si verifica questo errore?

+3

Hi! So che sei in giro da un po 'ma non hai fatto molte domande. Non ci sono abbastanza informazioni qui per rispondere alla tua domanda; per favore leggi [chiedi]. In particolare, quella linea della configurazione del modulo sembra ok, ma abbiamo bisogno di vedere il ** costruttore per DemoClient.class ** e il ** codice DemoAPIFactory **. Per favore [modificali] nella tua domanda. – durron597

+1

Grazie durron, ho aggiornato tutte le informazioni. – almy

+0

Avete 'DemoApi' definito da qualche altra parte? Ad esempio 'bind (DemoApi.class)'? Cerca ovunque in tutti i tuoi moduli, sicuramente lo hai da qualche parte. –

risposta

30

Questo è un problema comune, la soluzione è documentata nella 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) { 
    ... 
    } } 
+0

Grazie Jan. Avevo completamente perso quella parte sulla documentazione. E 'davvero hepled – almy

+0

Contento di aver potuto aiutare. Se ha risolto il tuo problema, dovresti accettare la risposta. –