0
votes

I am upgrading play framework app from 2.4.6 to 2.5.x. There are several occurrences where I call helper methods which belongs to some object. These helper methods use play's built-in classes(for example play.api.Play.current.configuration.underlying.getString) to get the job done.

I get following warning: "method current in object Play is deprecated: This is a static reference to application, use DI instead"

If I face this problem in class method then I can use dependency injection. How to deal with such a situation where method belongs to object and I am warned to use DI?

1
If you use dependency injection you can just change your object to class and it will do the work. Annotate it with @Singletonstsatlantis
Problem with this approach is if I want to use helper method(after changing object to class with @Singleton annotation), play gives following error: object ClientHandler is not a member of package controllers [error] Note: class ClientHandler exists, but it has no companion object. [error] controllers.ClientHandler.getClientUrlShailesh

1 Answers

2
votes

Play Framework usually provides a class you can inject instead of using the old static references.

For example, the below would mean you can stop using Play.current.configuration and DB:

import javax.inject.Inject
import play.api.db.Database
import play.api.Configuration

class MyClass @Inject() (configuration: Configuration, db: Database) {
   ...
}