2016-04-25 75 views
7

我目前使用Play.current的方式如下。Play.current在播放中已棄用​​2.5

import play.api.{Logger, Play} 

object ApplicationConfig { 

    val app = Play.current 
    def getConfInt(key: String): Option[Int] = { 
    val result = app.configuration.getInt(key) 
    result 
    } 
} 

自從遷移到2.5,我已經警告說,它已被棄用與

「這是一個靜態的參考應用程序,使用DI而不是」

然而, doc並不完全說我該如何使用DI。

感謝

回答

6

根據您的使用情況,你現在應該使用EnvironmentApplicationLifecycleConfiguration代替Application

在你的情況是在配置居然感興趣,所以要做到這一點在打球的方式2.5.x會是這樣的:

class HomeController @Inject() (configuration: play.api.Configuration) extends Controller { 

    def config = Action { 
    Ok(configuration.underlying.getInt("some.config.key")) 
    } 

} 

我提供的例子是爲控制器,但你可以使用這個也可以在你的應用程序的其他地方使用。我只是不喜歡你提供的ApplicationConfig對象 - 在遷移到Play 2.5.x時考慮重構它 - DI現在是要走的路

+0

是否可以這樣做: configuration.getInt(「some.config.key 「).getOrElse(-1)? – Scipion

+0

這非常好!你可以在這裏看到API文檔:https://www.playframework.com/documentation/2.5.x/api/scala/index.html#[email protected]%28path:String%29:Option[Int] – Anton