我一直试图让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.我在哪里错了?