2015-04-23 36 views
10

AssistedInject ile ilgili bir sorunum var. Bu bağlantıyı https://github.com/google/guice/wiki/AssistedInject yönergeleri takip ama benim uygulamayı çalıştırdığınızda bir hata alıyorum: Burada Guice assistedinject zaten yapılandırılmış

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) 

benim modülü yapılandırması:

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

benim fabrika nasıl göründüğünü:

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

arayüzü şu şekilde ifade edilir:

Burada
public interface DemoAPI { 
    //list of interface methods 
} 

Veuygulama ben dropwizard guice paket kullanıyorum

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

olduğunu.

Bu hata neden oluyor?

+3

Hi! Bir süredir olduğunu biliyorum ama pek çok soru sormadın. Sorunuzu cevaplamak için yeterli bilgi yok; lütfen [sor] 'u okuyun. Özellikle, modül yapılandırmanızın o satırı iyi görünüyor, ancak DemoClient.class ** ve ** DemoAPIFATIC kodu ** için yapıcıyı görmemiz gerekiyor. Lütfen onları sorunuza göre düzenleyin. – durron597

+1

Teşekkür ederim durron, tüm bilgileri güncelledim. – almy

+0

Başka bir yerde tanımlı 'DemoApi' var mı? Bağlama (DemoApi.class) 'gibi? Tüm modüllerinizde her yere bak, kesinlikle bir yere sahipsin. –

cevap

30

Bu çözelti javadoc ortak bir sorun belgelenmiştir edilir:

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

Teşekkür ederim Jan. Belgelerdeki o bölümü tamamen kaçırdım. Gerçekten hepimiz yardımcı olabilir – almy

+0

Sevindim. Sorunu çözdüyse, cevabı kabul etmelisin. –