Paste into scala object

I am using Play framework 2.5 and trying to inject a WSClient into the scala object used in my controllers.

import play.api.libs.concurrent.Execution.Implicits.defaultContext

    object MyObject {

        @Inject
        var ws: WSClient = null

        def doSomething() = { // use wsclient }

    }

I use MyObject in several controllers and when doSomething () is called, wsclient is null.

+8
source share
1 answer

You must define the class MyObjectas a class and enter wsclient into it:

class MyObject @Inject()(ws: WSClient) {
    def doSomething() = { /* use wsclient */ }
}
+3
source

All Articles