2017-07-04 2 views
2

私は、プレイのConfigurationインスタンスとWSClientインスタンスに依存するapiサービスクラスを持っています。Macwireでプレイの依存関係を設定できない

と私はMacwireとコンパイル時の注入を使用したい@Inject()注釈の原因を使用したくないので、私がやったことはこれです:

// this is a trait that here im wiring all the dependencies that my api service needs 
trait ApiDependencies { 

    lazy val conf: Configuration = wire[Configuration] 
    lazy val wsc: WSClient = wire[WSClient] 

} 


// this is the api service 

class ApiService extends ApiDependencies { 

    def getInfo (id: String): Future[Option[Info]] = { 
    wsc.url("...").withHttpHeaders(("Content-Type", "application/json")).get.map { response => 
     response.status match { 
     case Status.OK => ... 
     case Status.NO_CONTENT => ... 
     case _ => throw new Exception() 
     } 
    } 
    } 
} 

が、私は、コンパイラのエラーを取得:

Error: Cannot find a value of type: [com.typesafe.config.Config]
lazy val conf: Configuration = wire[Configuration]

Error: Cannot find a public constructor nor a companion object for [play.api.libs.ws.WSClient] lazy val wsc: WSClient = wire[WSClient]

誰かがこの問題を解決する方法を知っていますか?なぜ起こっているのですか:/

ありがとう!

+1

[MacWireでサービスに依存関係を注入する方法(フレームワークをプレイする方法)](https://stackoverflow.com/questions/44875361/how-to-inject-dependencies-to-a-service-with- –

答えて

0

Configurationは、internally usesタイプセーフのConfig libraryであるplayframeworkコンフィグレーションです。 Playframework docsを引用:

The configuration file used by Play is based on the Typesafe config library

あなたが取得している例外は、まさにこのことをあなたに伝えます - macwireにはConfigインスタンスがスコープ内に存在しないようConfigurationのインスタンスを作成することができません。

これを修正するには、明らかにそのようなインスタンスを用意する必要があります。そうするための最も簡単な方法は、おそらく次のようになります。ConfigFactory.Load()は基本的にデフォルトの設定ファイルを使用していること

import com.typesafe.config.{Config, ConfigFactory} 
trait ApiDependencies { 
    lazy val configuration: Config = ConfigFactory.load() 
    lazy val conf: Configuration = wire[Configuration] 
} 

注(application.conf)と、それそれは実際にによって提供されては、Play's Configuration docsに記載された技術をオーバーライドアカウントの設定に時間がかかります(タイプセーフコンフィグGitHubののreadmeから)タイプセーフ設定ライブラリ:

users can override the config with Java system properties, java -Dmyapp.foo.bar=10


WSClientについて:トン彼はWSClientがクラスではなく、a traitという事実のためです。あなたは、このような実際の実装、すなわちNingWSClientを、配線する必要があります。

trait ApiDependencies { 
    lazy val conf: Configuration = wire[Configuration] 
    lazy val wsc: WSClient = wire[NingWSClient] 
} 

は(「既知の実装クラス」の下の)クラスを実装のリストについてWSClient scaladocを参照してください - を書いている時点でのみNingWSClientAhcWSClientがあります。どちらが優れているかは、異なる(また、意見に基づく可能性のある質問です)。

+0

WSClientのエラーに対する答えがありますか?これを取得するには/ @ J0HN – JohnBigs

+0

はい、WSClientのエラーを修正する方法を知っていますか? –

+0

@JohnBigs私は答えを更新しました。一度見て、それが役に立ったら教えてください – J0HN

関連する問題