Play 2.5.X: the current method in the Play object is deprecated: this is a static link to the application, use DI instead

I am new to PlayFramework.

Please give me an example of how to access the configuration options from my view. I am using PlayFramework 2.5.3

Old way (@current deprecated):

@current.configuration.getString("environment.param")

New way (as far as I understand, the configuration should be introduced):

I know how to access it from the controller.

@Inject() (val messagesApi: MessagesApi, configuration: Configuration)

How to use it from my view?

+4
source share
3 answers

, . , DI Play, . :

  • Inject Configuration
  • implicit /

    class Application @Inject() (implicit val config: Configuration) extends Controller {
    
        def index = Action {
            Ok(views.html.index("foo"))
        }
    }
    

:

@(myParam1: Any)(implicit config: Configuration)
<h2>Some HTML here @myParam1 @config.getString("environment.param")</h2>

, - DI, , .

+9

java, application.conf 2.5, , :

public class HomeController extends Controller {
private Configuration configuration;

@Inject
public HomeController(Configuration configuration) {
    this.configuration = configuration;
}

public Result index() {
    String value = configuration.getString("key");
    System.out.println("value of key is " + key);
    return ok(value);
}

}

Configuration , .

see also this discussion: DI for java in game 2.5

0
source

All Articles