How does @Inject in Scala work

11,061

Looks close. Change val to var because it is not final and needs to be injected at a latter stage.

@Inject var mailer: MailerClient = null

I'd check also that the MailerClient library is mentioned as a dependency in the project configuration. You could try with WSClient instead as it's included by default in the template:

@Inject var ws: WSClient = null

Especially as I know that this particular one works.

Update

Created a demo on GitHub which is the Play-Scala template with the index method changed as follows:

import play.api._
import play.api.libs.ws.WSClient
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits.defaultContext

class Application extends Controller {

  @Inject var ws: WSClient = null

  def index = Action.async {
    ws.url("http://google.com").get.map(r => Ok(r.body))
  }

}
Share:
11,061

Related videos on Youtube

Haito
Author by

Haito

I’ve been programming for quite some time. I’ve used many popular languages sucha as C++, C# and Java, but they didn’t convinced me. After only few weeks I was tired of omnipresent boilerplate… And then Chriss told me about Martin Odersky’s child, Scala. It was a bit better, far more powerfull but inredibly annoying in the long run. Few months later he told me about Elixir, brand new language with ugly do end syntax that supposed to be amazing. And it indeed was. Elixir had things that scala was lacking. It had simplicity and underlying powerfull VM with all of the Erlang legacy. That’s where I’m now. I am very happy Elixir developer wannabe. Phoenix WebFramework completely blew me away with it’s simple, yet powerfull design. If I could tell you to learn one single thing, I would tell you to learn Phoenix… And yeah, I totally didn't pasted it here just to get the achievement...

Updated on June 04, 2022

Comments

  • Haito
    Haito almost 2 years

    I'm wondering how does @Inject annotation in Play-Scala works. It obviously injects a dependency, but I'm curious how is it working. When I was using it on class extending controller and set routes generator to injectroutesgenerator it seems to autmagically create objects from those classes, but how do I use it in other context?

    I tried:

    @Inject val mailer: MailerClient = null
    

    But that doesn't seem to work. Are there any posibilities to @Inject things (that mailerClient, WS ets.) directly to a value, not to controller class?

  • Haito
    Haito almost 9 years
    It seems legit but still nullPointerException
  • Haito
    Haito almost 9 years
    When inject MailerClient into class it work as dream, but when i do it like that: "@Inject var mailer: MailerClient = null" github.com/Hajtosek/mailTest/blob/master/app/service/Mail.sc‌​ala
  • Haito
    Haito almost 9 years
    So you're telling me that i can ONLY use @Inject on Controllers?
  • bjfletcher
    bjfletcher almost 9 years
    You can use it in classes or traits, not objects but you can use the @Singleton annotation for a similar behaviour - docs.oracle.com/javaee/7/api/javax/inject/Singleton.html
  • Haito
    Haito almost 9 years
    Thank you very much! You helped me a lot.
  • Haito
    Haito almost 9 years
  • bjfletcher
    bjfletcher almost 9 years
    That's fine, you'll need to tell the system about the injectable module (@Singleton by itself isn't enough). Have a look at this for a Play 2.4 specific example: stackoverflow.com/a/30959808/722180 Have a look also at this for more Scala ways of doing it: stackoverflow.com/a/24823300/722180