为什么我得到"类必须有一个(并且只有一个)构造函数"错误?

时间:2016-06-28 18:12:38

标签: java dependency-injection guice

我一直试图让Guice工作但最终得到这个:

  

类必须有一个(且只有一个)构造函数

我的界面:

public interface AddrBookStore {
    public Contact getContactByKey(String key);
    public void addContact(Contact c);
}

实施:

public class RdbmsBasedAddrBookStore implements AddrBookStore {
    private Connection connection;

    public RdbmsBasedAddrBookStore(Connection connection) {
        this.connection = connection;
    }

    @Override
    public Contact getContactByKey(String key) throws AddrBookException
    {} 
    @Override
    public void addContact(Contact c) throws AddrBookException
    {}
}

绑定模块:

public class ABguiceConfingModule extends AbstractModule {
    @Override
    protected void configure() {        
        bind(AddrBookStore.class).to(RdbmsBasedAddrBookStore.class);
    }
}

我注射的AddrBook客户端:

public class AddrBook {
    private AddrBookStore store;

    @Inject
    public AddrBook(AddrBookStore store)
    {
        this.store = store;
    }
    ... other methods;
}

我的主要人物:

public class App 
{
    public static void main( String[] args ) throws Exception
    {
        Injector injector = Guice.createInjector(new ABguiceConfingModule() );
        AddrBookStore store = injector.getInstance( AddrBookStore.class );

        AddrBook book = new AddrBook(store);
        AddrBookCLI cli = new AddrBookCLI(book);
        cli.interact(new InputStreamReader(System.in), new OutputStreamWriter(System.out));

}}

毕竟,我收到了这个错误:

1) Could not find a suitable constructor in addrbook.store.RdbmsBasedAddrBookStore. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
[ERROR] at addrbook.store.RdbmsBasedAddrBookStore.class(RdbmsBasedAddrBookStore.java:23)
[ERROR] at addrbook.ABguiceConfingModule.configure(ABguiceConfingModule.java:13)

我有使用Spring的经验,而不是Guice。我在哪里错了?

1 个答案:

答案 0 :(得分:6)

您尚未设置AddrBookStore的主要依赖关系。您需要拥有Connection的绑定,然后需要使用@Inject注释构造函数。

您已经设置了AddrBookStore课程但很明显它已经包装了一个Rdbms ...除非您没有设置Rdbms

在Guice中有很多方法可以做到这一点,在这种情况下我可能会用Provider<Connection>来做这个,这样你就有了一个完整的类来放置代码来启动你与数据库的连接,所以像:

public class ConnectionProvider implements Provider<Connection> {
    public Connection get() {
        // put your code that connects to the database here
    }
}

然后你的模块将是:

public class ABguiceConfingModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(AddrBookStore.class).to(RdbmsBasedAddrBookStore.class);
        bind(Connection.class).toProvider(ConnectionProvider.class);
    }
}

最后是你的AddrBookStore

public class RdbmsBasedAddrBookStore implements AddrBookStore {
    private Connection connection;

    @Inject
    public RdbmsBasedAddrBookStore(Connection connection) {
        this.connection = connection;
    }

    @Override
    public Contact getContactByKey(String key) throws AddrBookException
    {}

    @Override
    public void addContact(Contact c) throws AddrBookException
    {}
}