Google guice的实施不起作用

时间:2015-11-29 18:22:05

标签: guice roboguice guice-servlet guice-3 guice-persist

我是Google guice的初学者。 我编写了如下所示插入Oracle和postgres数据库,我在这里发布了一个非常简单的同上,但是,当我运行这个时,我收到错误

线程“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);
    }

}

请帮助解决它。 感谢

1 个答案:

答案 0 :(得分:0)

正如错误所述,ConsumerContract没有绑定。 Guice不进行类路径扫描,因此它不知道ConsumerClass是ConsumerContract的正确实现。您需要在InjectModule中添加一个绑定...

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

同样适用于您希望Guice为您构建的所有实现类。