Scala (Play 2.4.x) How to call a class with @inject() annotation

前端 未结 4 1982
臣服心动
臣服心动 2021-02-06 05:29

I\'m looking at the scaly code example from play-mailer: https://github.com/playframework/play-mailer

It goes basically like this:

class MyComponent @         


        
4条回答
  •  醉话见心
    2021-02-06 05:56

    ( I don't have enough reputation to comment, so posting as an answer)

    The answer posted by @aparo should be marked as the corect/approved answer, because it does solve the problem. You said that this doesn't solve the original question because it moves the dependency down to another class, but this is only partially true, as that other class will only need to provide you with a MyComponent instead of a MailerClient. Although dependency injection needs to get used all the way down to the final controller (In the case of Play), you usually won't have to inject more one single object.

    Proof of this can be seen in a question I posted, as I had the same mindset as you at the time. In my example, my controller needs only a UserSearch dependency, the DatabaseConfigurationProvider dependency is dealt with by guice, so I don't need to state it anywhere again.

提交回复
热议问题