简体   繁体   English

如何使用MacWire(播放框架)向服务注入依赖项

[英]how to inject dependencies to a service with MacWire (play framework)

I have a service class, and the service have one method getSomethingFromApi , now , I want to have play Configuration instance so I can pull stuff from the application.conf, and a play WSClient so I can perform http calls. 我有一个服务类,该服务有一个方法getSomethingFromApi ,现在,我想拥有一个播放Configuration实例,以便可以从application.conf中提取内容,以及一个播放WSClient,以便可以执行http调用。

this is how I want my service to look: 这就是我希望我的服务看起来的样子:

class MyApiService {

  def getSomethingFromApi(whichApi: String): Future[ApiRes] = {
    wsClient.url(configuration.getString(whichApi)).withHttpHeaders(("Content-Type", "application/json")).get.map { res =>
      response.status match {
        case Status.OK => // do something
        case _ => throw new Exception
      }
    }
  }

}

and this is the ServicesModule that is wiring my services: 这是连接我的服务的ServicesModule:

import com.softwaremill.macwire._

trait ServicesModule {

  lazy val myService: MyApiService = wire[MyApiService]

}

my question now is what is the right way of using wiring play Configuration and WSClient instances..? 我的问题是,使用连线配置和WSClient实例的正确方法是什么? cause currently i need those instances in my service but i dont have them, how should i do this the right way? 因为当前我需要在我的服务中使用那些实例,但是我没有它们,我该怎么做才正确? thanks 谢谢

With macwire it'll probably look like this 使用macwire,它可能看起来像这样

// MyApiService.scala
class MyApiService(wsClient: WSClient) { ... }

// ServicesModule.scala
trait ServicesModule with NingWSComponents {
    lazy val wsClient = wire[WSClient]
    lazy val apiService = wire[MyApiService]
}

I haven't tried using macwire with play myself, so I have relatively low confidence that it'll work on the first try, but macwire play example suggests mixing in certain Play modules to provide values needed for WSClient. 我还没有尝试自己将macwire与play一起使用,因此我对它可以在第一次尝试中使用的信心相对较低,但是macwire play示例建议在某些Play模块中混合以提供WSClient所需的值。 Most likely not all of them are needed, but some might be - soo I'd suggest starting with just NingWSComponents and gradually adding more until it works. 很可能不需要全部,但是有些可能需要-所以我建议仅从NingWSComponents开始,然后逐渐添加更多,直到它起作用为止。

For the configuration I suggest using something like PureConfig and load the configuration as follows 对于配置,我建议使用PureConfig之类的东西 ,并按如下所示加载配置

import pureconfig._
import pureconfig.error.ConfigReaderFailures

case class YourConfClass(name: String, quantity: Int)

val config: Either[pureconfig.error.ConfigReaderFailures,YourConfClass] = loadConfig[YourConfClass]

This then can be passed on to any component of your app using macwire. 然后可以使用macwire将其传递到应用程序的任何组件。

As of Play 2.6.X one should use AhcWSComponents that are provided by the ws dependency as follows: 从Play 2.6.X开始,应使用ws依赖项提供的AhcWSComponents ,如下所示:

In your build.sbt file add the ws dependency to your project 在build.sbt文件中,将ws依赖项添加到项目中

libraryDependencies += ws

In your module trait mix-in the AhcWSComponents trait and wire the WSClient 在模块特征中,混入AhcWSComponents特征,并连接WSClient

trait ServicesModule with AhcWSComponents {
    lazy val wsClient = wire[WSClient]
    lazy val apiService = wire[MyApiService]
}

In your MyApiServic e add the WSClient as a param. 在您的MyApiServic E添加的WSClient作为PARAM。 to the constructor 给构造函数

class MyApiService(wsClient: WSClient) { ... }

And now you're done. 现在您完成了。 This general rule applies to all provided dependencies. 该通用规则适用于所有提供的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM