2015-11-29 6 views
0

Я начинаю искать Google. Я запрограммировал, как показано ниже, чтобы вставить в Oracle и postgres db, я разместил здесь очень простое коеде, но, когда я запускаю это, я получаю сообщение об ошибкеВыполнение Google guice не работает

Исключение из темы "main" com.google.inject. ConfigurationException: ошибки конфигурации Guice:

1) Никакой реализации для com.googleguice.contract.ConsumerContract был связан. при размещении com.googleguice.contract.ConsumerContract

1 ошибка на com.google.inject.internal.InjectorImpl.getProvider (InjectorImpl.java:1004) на com.google.inject.internal.InjectorImpl.getProvider (InjectorImpl.java:961) на com.google.inject.internal.InjectorImpl.getInstance (InjectorImpl.java:1013) на com.googleguice.client.ClientClass.main (ClientClass.java:15)

package com.googleguice.contract; 

import com.google.inject.ImplementedBy; 


public interface ServiceContract { 
public void Insertion(boolean b); 

} 


package com.googleguice.serviceclasses; 


import javax.inject.Singleton; 

import com.googleguice.contract.ServiceContract; 

@Singleton 
public class InsertOracle implements com.googleguice.contract.ServiceContract{ 

    @Override 
    public void Insertion(boolean b) { 
     // TODO Auto-generated method stub 
     if(b){ 
      System.out.println(b +"inserted in Oracle DB"); 
     }else 
      System.out.println("not inserted in Oracle DB"); 

    } 

} 



package com.googleguice.serviceclasses; 

import javax.inject.Singleton; 

import com.googleguice.contract.ServiceContract; 

@Singleton 
public class InsertPostgres implements com.googleguice.contract.ServiceContract{ 

    @Override 
    public void Insertion(boolean a) { 
     // TODO Auto-generated method stub 
     if(a){ 
      System.out.println(a +"inserted in postgres DB"); 
     }else 
      System.out.println("not inserted in postgres DB"); 

    } 

}

package com.googleguice.consumerclass; 



import com.google.inject.Inject; 
import com.google.inject.name.Named; 
import com.googleguice.contract.ServiceContract; 

public class ConsumerClass implements com.googleguice.contract.ConsumerContract { 
    public ServiceContract sc; 
    @Inject 
    public void ConsumerClass(ServiceContract s){ 
     this.sc=s; 
    } 

    @Override 
    public void accessingServices(boolean a) { 
     // TODO Auto-generated method stub 
     this.sc.Insertion(a); 
    } 

} 





package com.googleguice.module; 

import com.google.inject.AbstractModule; 
import com.google.inject.Binder; 
import com.google.inject.Module; 
import com.google.inject.name.Names; 
import com.googleguice.consumerclass.ConsumerClass; 
import com.googleguice.contract.ConsumerContract; 
import com.googleguice.contract.ServiceContract; 
import com.googleguice.serviceclasses.InsertOracle; 
import com.googleguice.serviceclasses.InsertPostgres; 

public class InsertModule extends AbstractModule { 
    @Override 
    protected void configure() { 
     // TODO Auto-generated method stub 
     bind(ServiceContract.class).to(InsertPostgres.class); 

    } 

} 






package com.googleguice.client; 

import com.google.inject.Guice; 
import com.google.inject.Injector; 
import com.googleguice.contract.ConsumerContract; 
import com.googleguice.contract.ServiceContract; 
import com.googleguice.module.InsertModule; 

public class ClientClass { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Injector i = Guice.createInjector(new InsertModule());   

      ConsumerContract cc = i.getInstance(ConsumerContract.class); 

      cc.accessingServices(true); 
    } 

} 

Помощь Pls для решения проблемы. Thanks

ответ

0

Как говорится в ошибке, для ConsumerContract нет привязки. Guice не выполняет сканирование классов, поэтому он не знает, что ConsumerClass является правильной реализацией для ConsumerContract. Вам нужно добавить привязку в InjectModule ...

bind(ConsumerContract.class).to(ConsumerClass.class); 

Аналогично для всех классов реализации, которые вы собираетесь для Guice построить для вас.

+0

Привет, я понял свою ошибку, он работает, когда я попытался использовать ConsumerContract cc = i.getInstance (ConsumerClass.class); в клиентском классе он работает, я пытался создать объект интерфейса вместо объекта класса. кстати спасибо –