I have a Client class that makes an API call to an external vendor. In my controller, I inject the client as follows.
@Singleton
class AdsController @Inject()(client: MyClient)(
implicit ec: ExecutionContext
) extends InjectedController {
def index = Action.async(json.parse[ClientRequest]) {
client.send(request.body).map({
case s: SuccessResponse => Ok(Json.toJson(s))
...
})
}
}
The client class looks as follows
class MyClient @Inject()(ws: WSClient, appConfig: Configuration)
(implicit ec: ExecutionContext) {...}
I wanted to understand two things
- Does the injector inject a new instance of MyClient for every request?
- If yes, Does the injector inject a new instance of WSClient and Configuration every time?
If both are yes then injecting configuration unnecessarily creates new instances which won’t be a good idea.