How to Inject() into a class with constructor parameters?

Viewed 2223

I have a WebSocket controller which creates per connection actor handler:

class WebSocketController @Inject()(cc: ControllerComponents)(implicit exc: ExecutionContext) {
  def socket: WebSocket = WebSocket.accept[JsValue, JsValue] { request =>
    ActorFlow.actorRef { out => // Flow that is handled by an actor from 'out' ref
      WebSocketActor.props(out) // Create an actor for new connected WebSocket
    }
  }
}

And inside the actor handler I need to work with ReactiveMongo:

trait ModelDAO extends MongoController with ReactiveMongoComponents { 
  val collectionName: String
  ...
}
class UsersCollection @Inject()(val cc: ControllerComponents,
                                val reactiveMongoApi: ReactiveMongoApi,
                                val executionContext: ExecutionContext,
                                val materializer: Materializer)
  extends AbstractController(cc) with ModelDAO {
  val collectionName: String = "users"
}

So, the usual way is to @Inject() UsersCollection in the target class. But I can't do something like:

class WebSocketActor @Inject()(out: ActorRef, users: UsersCollection) extends Actor { ... }

Because instances of actor creates inside WebSocketActor companion object:

object WebSocketActor {
  def props(out: ActorRef) = Props(new WebSocketActor(out))
}

How do I use UsersCollection inside the WebSocketActor?

2 Answers
Related