java 为什么我会收到“类必须有一个(并且只有一个)构造函数”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38083434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:05:58  来源:igfitidea点击:

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

javadependency-injectionguice

提问by Kartz Kartel

I have been trying to get Guice working but end up with this:

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

Classes must have either one (and only one) constructor

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

My interface:

我的界面:

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

The implementation:

实施:

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

The binding module:

绑定模块:

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

The AddrBookclient where I am injecting:

AddrBook我注射的客户端:

public class AddrBook {
    private AddrBookStore store;

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

And my main:

而我的主要内容:

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));

}}

After all this, I'm getting this error:

毕竟,我收到此错误:

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)

I have experience with Spring and not with Guice. Where am I going wrong here?

我有 Spring 而不是 Guice 的经验。我哪里出错了?

采纳答案by durron597

You haven't set up the primary dependency for AddrBookStore. You need to have a binding for Connection, and then you need to annotate the constructor with @Inject.

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

You've set up the AddrBookStoreclass but clearly it's wrapping an Rdbms... except you haven't set up the Rdbms.

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

There are lots of ways to do this in Guice, in this case I would probably do it with a Provider<Connection>, that way you have an entire class to put the code for spinning up your connection to the database, so something like:

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

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

Then your module would be:

那么你的模块将是:

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

And then finally your AddrBookStore:

最后你的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
    {}
}