Error trying to inject a dependency in Lagom

629 views Asked by At

I'm trying to create a simple service to send emails using Lagom framework and the Scaladsl. I'm trying to use the Play Mailer Plugin to handle emails but I'm struggling trying to inject it into the service implementation.

I created the service trait and implementation passing the mailerClient as a dependency in the constructor.

trait MailerService extends Service { ... }

class MailerServiceImpl(mailerClient: MailerClient, persistentEntityRegistry: PersistentEntityRegistry) extends MailerService {
...
}

I'm wiring the service in the ApplicationLoader, following the pattern explained in the Lagom documentation and in the hello world application using macwire.

abstract class MailerApplication(context: LagomApplicationContext) 
  extends LagomApplication(context)
    with CassandraPersistenceComponents
    with AhcWSComponents {

  override lazy val lagomServer: LagomServer = serverFor[MailerService](wire[MailerServiceImpl])
  override lazy val jsonSerializerRegistry = MailerSerializerRegistry
  persistentEntityRegistry.register(wire[MailEntity])
}

When I try to compile, I get the following error.

[error] /.../workspace/mailer/mailer-impl/src/main/scala/com/example/mailer/impl/MailerApplicationLoader.scala:92: Cannot find a value of type: [play.api.libs.mailer.MailerClient]

I thought macwire would be able to sort out the dependencies from the constructor but it looks like it's not really. I've tried different options, like trying to wire it explicitly in the application loader without any success so far.

I'm pretty sure there's something I'm getting wrong about how DI works in Lagom but I cannot figure it out.

Any ideas?

1

There are 1 answers

0
Tim Moore On BEST ANSWER

For the MailerClient to be injectable, you need to mix in the MailerComponents trait along with the other traits you use in your service.

For example:

// ...
import play.api.libs.mailer._

abstract class MailerApplication(context: LagomApplicationContext) 
  extends LagomApplication(context)
    with MailerComponents // add this here
    with CassandraPersistenceComponents
    with AhcWSComponents {

  override lazy val lagomServer: LagomServer = serverFor[MailerService](wire[MailerServiceImpl])
  override lazy val jsonSerializerRegistry = MailerSerializerRegistry
  persistentEntityRegistry.register(wire[MailEntity])
}

This is described in the Play Mailer documentation on compile-time injection