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

前端 未结 4 1954
臣服心动
臣服心动 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 ran into the same problem. I want to create a class or object that has the mailing functionality and then I can call it whenever I want to send out emails from any controllers.

    I think what you were asking is equivalent to how to use the mailerclient outside play framework. So far as I understand, you cannot. Even you create a class in the app/controllers folder, it is just a regular scala class that has nothing to do with the magics in play framework. @Inject works only with controllers or modules. Because if you create a standalone class, you have to inject something by yourself when instantiate it. Unless you are building a module or extending a controller. Do you noticed that the AppController used to be object and now it is a class? You don't need to instantiate that when you use it. I believe the framework did something to instantiate it with configuration (injected).

    So if you want to achieve the original design goal, you either write a module, publish it, and then use it in all controllers; or use some other email libraries in scala to wrap up the email sending functionality.

提交回复
热议问题