I’m starting a port from Casbah to the supported Mongo driver. I’m getting the following exception when trying to inject it in a Play app that uses runtime DI.
1 error
com.google.inject.CreationException: Unable to create injector, see the following errors:
1) Error injecting constructor, java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getApplicationName()Ljava/lang/String;
at database.MongoEnvironmentImpl.<init>(MongoEnvironment.scala:22)
at database.MongoEnvironmentImpl.class(MongoEnvironment.scala:22)
while locating database.MongoEnvironmentImpl
while locating database.MongoEnvironment
for the 5th parameter of Tyrion.<init>(Tyrion.scala:19)
at Module.configure(Module.scala:17) (via modules: com.google.inject.util.Modules$OverrideModule -> Module)
while locating Tyrion
It’s trying to inject the MongoEnvironment, 5th parameter…
@Singleton
class Tyrion @Inject()(config: Configuration,
lifecycle: ApplicationLifecycle,
akka: ActorSystem,
legacyMongo: LegacyMongoEnvironment,
mongo: MongoEnvironment,
es: EncryptionService,
implicit val ec: ExecutionContext) {
The Module
binding simply adds another line next to the Casbah driver… (3rd line in the configure
method)
class Module extends AbstractModule {
override def configure(): Unit = {
bind(classOf[Tyrion]).asEagerSingleton()
bind(classOf[LegacyMongoEnvironment]).to(classOf[LegacyMongoEnvironmentImpl])
bind(classOf[MongoEnvironment]).to(classOf[MongoEnvironmentImpl])
bind(classOf[EncryptionService]).to(classOf[KaliumEncryptionService])
bind(classOf[Emailer]).to(classOf[SMTPMailer])
bind(classOf[Messenger]).annotatedWith(Names.named("sms")).to(classOf[SmsMessenger])
bind(classOf[Messenger]).annotatedWith(Names.named("voice")).to(classOf[VoiceMessenger])
}
}
And lastly, the MongoEnvironment:
trait MongoEnvironment {
val db: MongoDatabase
}
@Singleton
class MongoEnvironmentImpl @Inject()(config: Configuration) extends MongoEnvironment {
private val server = config.getOptional[String]("tyrion.mongo.server").getOrElse("localhost")
private val port = config.getOptional[Int]("tyrion.mongo.port").getOrElse(27017)
private val database = config.getOptional[String]("tyrion.mongo.database").getOrElse("tyrion")
private val codecRegistry = fromRegistries(fromProviders(classOf[Invitation2]), DEFAULT_CODEC_REGISTRY)
private val mongoClient = MongoClient(s"mongodb://$server:$port")
override val db = mongoClient.getDatabase(database).withCodecRegistry(codecRegistry)
}
It throws on the second last line, as per the stack trace. It can’t find the ConnectionString
class. Wondering if anyone has experienced this issue before?