Dependency injection issue for ReactiveMongo in Play 2

I’m building a web app using Play 2.6 and ReactiveMongo 0.13. But when I try to run the app, I get this output:

ProvisionException: Unable to provision, see the following errors:

1) No implementation for play.modules.reactivemongo.ReactiveMongoApi was bound.
  while locating play.modules.reactivemongo.ReactiveMongoApi
    for the 2nd parameter of controllers.Employees.<init>(Employees.scala:17)
...

1 error

The class this error refers to is declared with this constructor:

class Employees @Inject() (cc: ControllerComponents,
                           val reactiveMongoApi: ReactiveMongoApi,
                           implicit val materializer: akka.stream.Materializer
                          )

ReactiveMongo is specified and is supposed to be injected.

I read on the web that the solution is to add the following line to application.conf :

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"

The thing is that I did that, but I still get the dependency injection error. I’m not sure where to proceed from here.

I took a look at the definition of ReactiveMongoModule.

It appears that it parses all the configuration and puts one @Named binding of ReactiveMongoApi per configuration entry. If the configuration is named default it will also get an un@Named binding.

Can you check if ReactiveMongo is configured properly?

I may be missing something basic here. I looked at the ReactiveMongo site many times and found no setup steps, it is supposed to work out of the box.

I did look up at the docs for application.conf more closely, though. I tried adding these lines, thinking that selecting Mongo as my main database would help, but it didn’t:

db.driver=reactivemongo.Driver
db.url="mongodb://localhost/collectionname"

But I don’t really see how that should make a difference to my current problem.

I looked up the Play documentation and I didn’t find anything about naming configuration, just naming application, or selecting running modes. My understanding is: as long as you don’t specify another configuration file to run, you’re always running the default configuration, and I am.

Clearly, I’m only starting with Play. The classes are recognized, which means that the libraries are loaded. Am I missing something obvious?

You were right! I had missed a part in the documentation. But ReactiveMongo uses its own key:

It was:

mongodb.uri="mongodb://localhost/collectionname"

Adding that line caused the dependency injection to activate.

I also had to leave:

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"

Both are needed, even though the second one is not in the documentation.

1 Like

Yes, that’s right. The ReactiveMongo module is totally separate from Play’s built-in database support, so you’ll have to refer to their documentation for setup. I do see the module mentioned here: http://reactivemongo.org/releases/0.1x/documentation/tutorial/play.html#play-24. Perhaps you were reading a different version of the docs.

You’d think the module would give you a useful error message if there are no data sources bound rather than a generic dependency injection error. Perhaps that’s an issue you can raise with them.