2015-07-19 62 views
1

什麼是在Scala中使用構造函數注入爲控制器注入多個依賴的正確方法(2.4.x,它提供了開箱即用的DI) ?Play Framework:在控制器中使用構造函數注入注入多個依賴

例如,

class ExampleController @Inject() (serviceOne: ServiceOne, serviceTwo: ServiceTwo) extends Controller { 
} 

上面不會編譯僅表示一個或沒有構造ARG可以注入。

無法找到任何有關如何使這項工作的良好參考。任何幫助表示讚賞。謝謝。

回答

0

您可能需要預先val的參數:

class ExampleController @Inject() (val serviceOne: ServiceOne, val serviceTwo: ServiceTwo) extends Controller { 

而且還檢查是否有正確的進口:

import javax.inject.Inject 

Here你也可以找到多個依賴一個例子,也許它有助於。

-1

您的代碼是正確的。此外這一點,你需要在你的application.conf

play.modules.enabled += "com.example.HelloModule" 

配置module然後在這個非常Module你需要描述你的依賴注入類:

import play.api.inject._ 

class HelloModule extends Module { 
    def bindings(environment: Environment, 
       configuration: Configuration) = Seq(
    bind[Hello].qualifiedWith("en").to[EnglishHello], 
    bind[Hello].qualifiedWith("de").to[GermanHello] 
) 
} 

For the offical documentation see this link.

相關問題