Skip to content
Advertisement

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

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

Classes must have either one (and only one) constructor

My interface:

JavaScript

The implementation:

JavaScript

The binding module:

JavaScript

The AddrBook client where I am injecting:

JavaScript

And my main:

JavaScript

After all this, I’m getting this error:

JavaScript

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

Advertisement

Answer

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.

You’ve set up the AddrBookStore class but clearly it’s wrapping an Rdbms… except you haven’t set up the 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:

JavaScript

Then your module would be:

JavaScript

And then finally your AddrBookStore:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement