I am a beginner lerner of Google guice. I have programmed like below to insert in Oracle and postgres db, I have posted a very simple coede here but, when I run this I am getting an error as
Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for com.googleguice.contract.ConsumerContract was bound. while locating com.googleguice.contract.ConsumerContract
1 error at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004) at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961) at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013) at 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 help to resolve it. Thanks
As the error says, there's no binding for ConsumerContract. Guice doesn't do classpath scanning so it doesn't know that ConsumerClass is the correct implementation for ConsumerContract. You'll need to add a binding in InjectModule...
Similarly for all the implementation classes which you intend for Guice to build for you.