为什么我会收到 "classes must have either one (and only one) constructor" 错误?

Why am I getting "classes must have either one (and only one) constructor" error?

我一直在努力让 Guice 正常工作,但结果是这样的:

Classes must have either one (and only one) constructor

我的界面:

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 的经验。我哪里错了?

您还没有为 AddrBookStore 设置主要依赖项。您需要对 Connection 进行绑定,然后您需要使用 @Inject.

注释构造函数

您已经设置了 AddrBookStore class 但显然它正在包装一个 Rdbms... 除了您还没有设置 Rdbms.

在 Guice 中有很多方法可以做到这一点,在这种情况下,我可能会用 Provider<Connection> 来做到这一点,这样你就有了整个 class 来放置用于旋转的代码您与数据库的连接,例如:

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
    {}
}

出现此错误消息是因为我忘记在构造函数上添加注释 @Injectpublic AddrBook(AddrBookStore store)在这个问题的情况下。