0

我是 Google guice 的初学者。我已经像下面这样编写了插入 Oracle 和 postgres db 的程序,我在这里发布了一个非常简单的 coede 但是,当我运行它时,我收到一个错误

线程“主”com.google.inject.ConfigurationException 中的异常:Guice 配置错误:

1) 没有绑定 com.googleguice.contract.ConsumerContract 的实现。同时定位 com.googleguice.contract.ConsumerContract

在 com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961) 在 com.google.inject.inject.internal.InjectorImpl 的 com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004) 1 错误.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);
    }

}

请帮助解决它。谢谢

4

1 回答 1

0

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

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

对于您打算让 Guice 为您构建的所有实现类也是如此。

于 2015-11-29T18:31:03.573 回答