本文整理汇总了Scala中play.api.inject.Binding类的典型用法代码示例。如果您正苦于以下问题:Scala Binding类的具体用法?Scala Binding怎么用?Scala Binding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Binding类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: PullRequestClientModule
//设置package包名称以及导入依赖的类
package de.stema.pullrequests.client
import de.stema.pullrequests.dto.ProjectDTO
import play.api.inject.{Binding, Module}
import play.api.{Configuration, Environment}
import scala.concurrent.Future
class PullRequestClientModule extends Module {
def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
if(configuration.getBoolean("offline").getOrElse(false)) {
bind[PullRequestClient].to[PullRequestSampleClient]
} else {
bind[PullRequestClient].to[PullRequestGitHubClient]
})
}
}
abstract class PullRequestClient {
def getPullRequests(configuration: String): Seq[Future[ProjectDTO]]
}
示例2: MailsendActorModule
//设置package包名称以及导入依赖的类
package modules
import play.api.{Configuration, Environment}
import play.api.inject.{Binding, Module}
import play.api.libs.concurrent.AkkaGuiceSupport
import actors.MailsendActor
import com.google.inject.AbstractModule
import schedulers.MailsendScheduler
class MailsendActorModule extends AbstractModule with AkkaGuiceSupport {
override def configure() = {
bindActor[MailsendActor]("mailsend-actor")
}
}
class MailsendScheduleModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
bind[MailsendScheduler].toSelf.eagerly
)
}
}
示例3: HelloWorldActorModule
//设置package包名称以及导入依赖的类
package modules
import play.api.{Configuration, Environment}
import play.api.inject.{Binding, Module}
import play.api.libs.concurrent.AkkaGuiceSupport
import actors.HelloWorldActor
import com.google.inject.AbstractModule
import schedulers.HelloWorldScheduler
class HelloWorldActorModule extends AbstractModule with AkkaGuiceSupport {
override def configure() = {
bindActor[HelloWorldActor]("hello-world-actor")
}
}
class HelloWorldScheduleModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
bind[HelloWorldScheduler].toSelf.eagerly
)
}
}
示例4: fakeApplication
//设置package包名称以及导入依赖的类
package controller
import com.google.inject.Inject
import org.flywaydb.core.Flyway
import org.flywaydb.core.internal.util.jdbc.DriverDataSource
import org.scalatestplus.play.FakeApplicationFactory
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.inject.{Binding, Module}
import play.api.{Application, Configuration, Environment}
trait MyApplicationFactory extends FakeApplicationFactory {
def fakeApplication(): Application = {
new GuiceApplicationBuilder()
.configure(Map("myapp.database.url" -> "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"))
.bindings(new FlywayModule)
.build()
}
}
class FlywayModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(bind[FlywayMigrator].toSelf.eagerly() )
}
}
class FlywayMigrator @Inject()(env: Environment, configuration: Configuration) {
def onStart(): Unit = {
val driver = configuration.underlying.getString("myapp.database.driver")
val url = configuration.underlying.getString("myapp.database.url")
val user = configuration.underlying.getString("myapp.database.user")
val password = configuration.underlying.getString("myapp.database.password")
val flyway = new Flyway
flyway.setDataSource(new DriverDataSource(env.classLoader, driver, url, user, password))
flyway.setLocations("filesystem:modules/flyway/src/main/resources/db/migration")
flyway.migrate()
}
onStart()
}
示例5: ConsulServiceLocatorModule
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.discovery.consul
import com.lightbend.lagom.javadsl.api.ServiceLocator
import play.api.{Configuration, Environment, Mode}
import play.api.inject.{Binding, Module}
import javax.inject.Singleton
import javax.inject.Inject
import javax.inject.Provider
import com.ecwid.consul.v1.ConsulClient
class ConsulServiceLocatorModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] =
if (environment.mode == Mode.Prod) Seq(
bind[ServiceLocator].to[ConsulServiceLocator].in[Singleton],
bind[ConsulConfig].to[ConsulConfig.ConsulConfigImpl],
bind[ConsulClient].toProvider[ConsulServiceLocatorModule.ConsulClientProvider]
)
else Seq.empty
}
object ConsulServiceLocatorModule {
private class ConsulClientProvider @Inject()(config: ConsulConfig) extends Provider[ConsulClient] {
override lazy val get: ConsulClient = new ConsulClient(config.agentHostname, config.agentPort)
}
}
示例6: BinableMapStringString
//设置package包名称以及导入依赖的类
package microtools.config
import java.time.Duration
import play.api.Configuration
import play.api.inject.{Binding, Module}
import scala.util.Try
class BinableMapStringString(underlying: Map[String, String]) extends Map[String, String] {
override def +[B1 >: String](kv: (String, B1)): Map[String, B1] = underlying + kv
override def get(key: String): Option[String] = underlying.get(key)
override def iterator: Iterator[(String, String)] = underlying.iterator
override def -(key: String): Map[String, String] = underlying - key
}
trait ConfigurationBindings { self: Module =>
def bindingsFor(configuration: Configuration, key: String): Seq[Binding[_]] = {
val configs: Configuration =
configuration.getConfig(key).getOrElse(Configuration.empty)
configs.keys.toSeq.flatMap(bindKey(configs, _))
}
def bindAsMap(configuration: Configuration, name: String, key: String): Seq[Binding[_]] = {
val configs: Configuration =
configuration.getConfig(key).getOrElse(Configuration.empty)
val map: Map[String, String] = configs.keys
.map(key => key -> configs.getString(key).getOrElse(""))
.toMap
Seq(
bind[BinableMapStringString]
.qualifiedWith(name)
.toInstance(new BinableMapStringString(map))
)
}
def bindKey(config: Configuration, key: String): Seq[Binding[_]] = {
val bindings = Seq.newBuilder[Binding[_]]
Try(config.underlying.getString(key)).foreach { value =>
bindings += bind[String].qualifiedWith(key).toInstance(value)
}
Try(config.underlying.getBoolean(key)).foreach { value =>
bindings += bind[Boolean].qualifiedWith(key).toInstance(value)
}
Try(config.underlying.getDuration(key)).foreach { value =>
bindings += bind[Duration].qualifiedWith(key).toInstance(value)
}
Try(config.underlying.getLong(key)).foreach { value =>
bindings += bind[Long].qualifiedWith(key).toInstance(value)
}
bindings.result()
}
}
示例7: CachecoolModule
//设置package包名称以及导入依赖的类
package de.zalando.cachecool.playmodule
import com.google.inject.name.Names
import com.typesafe.config.{ConfigObject, ConfigValue}
import de.zalando.cachecool.{CacheDefinition, GenericCache, CachecoolFactory}
import play.api.{Configuration, Environment, Logger}
import play.api.inject.{Binding, Module}
import scala.collection.JavaConverters._
class CachecoolModule extends Module {
private val logger = Logger(this.getClass.getName)
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
val factories = loadCacheFactories(environment, configuration)
val cachecoolCachesConfig: ConfigObject = configuration.getObject("cachecool.caches").getOrElse(throw new RuntimeException("Please provide cache configs"))
val caches = cachecoolCachesConfig.asScala
caches.flatMap { case (name, config) => buildCache(name, config, factories)
}.toSeq
}
def buildCache(name: String, config: ConfigValue, factories: Map[String, CachecoolFactory]): Seq[Binding[_]] = {
val cacheDef = CacheDefinition(config)
val factory = factories.get(cacheDef.factoryName).getOrElse(throw new RuntimeException(s"No factory is defined for ${cacheDef.factoryName}"))
val cache = factory.build(name, cacheDef.factoryConfig, factories)
val default = cacheDef.isDefault match {
case true => Some(bind(classOf[GenericCache]).toInstance(cache))
case _ => None
}
Seq(Some(bind(classOf[GenericCache]).qualifiedWith(Names.named(name)).toInstance(cache)), default).flatten
}
private def loadCacheFactories(environment: Environment, configuration: Configuration): Map[String, CachecoolFactory] = {
val factoriesDefinition = configuration.getObject("cachecool.factories").getOrElse(throw new RuntimeException("Please provide cache factories in config file")).asScala
(factoriesDefinition map {
case (name, className) => {
val clazz: Class[_] = environment.classLoader.loadClass(className.unwrapped().toString)
logger.info(s"Loading cache factory with name:$name and class:${clazz}")
val constructor = clazz.getConstructor(classOf[ClassLoader])
val factoryInstance: CachecoolFactory = constructor.newInstance(environment.classLoader).asInstanceOf[CachecoolFactory]
(name -> factoryInstance)
}
}).toMap
}
}
示例8: KamonPlayExtensionsModule
//设置package包名称以及导入依赖的类
package au.com.agiledigital.kamon.play_extensions
import javax.inject.{Inject, Provider}
import akka.actor.ReflectiveDynamicAccess
import play.api.{Logger, Configuration, Environment}
import play.api.inject.{Binding, Module}
import scala.util.{Failure, Success}
class KamonPlayExtensionsModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = Seq(
bind[FutureNameGenerator].toProvider[FutureNameGeneratorProvider],
bind[Metrics].to[MetricsSingleton].eagerly()
)
}
class FutureNameGeneratorProvider @Inject()(configuration: Configuration) extends Provider[FutureNameGenerator] {
private val configurationKey = "kamon.play-extensions.future-name-generator"
private val dynamics = new ReflectiveDynamicAccess(getClass.getClassLoader)
override def get(): FutureNameGenerator = {
configuration.getString(configurationKey) flatMap { fcqn =>
dynamics.createInstanceFor[FutureNameGenerator](fcqn, Nil) match {
case Success(generator) => Some(generator)
case Failure(error) =>
Logger.warn(s"Failed to create future namer generator [$fcqn] from [$configurationKey].", error)
None
}
} getOrElse new DefaultFutureNameGenerator()
}
}
示例9: CircuitBreakerModule
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.internal.javadsl.client
import javax.inject.{ Inject, Provider, Singleton }
import akka.actor.ActorSystem
import com.lightbend.lagom.internal.client.{ CircuitBreakerConfig, CircuitBreakerMetricsProviderProvider, CircuitBreakers }
import com.lightbend.lagom.internal.spi.CircuitBreakerMetricsProvider
import com.lightbend.lagom.javadsl.client.CircuitBreakersPanel
import play.api.inject.{ Binding, Module }
import play.api.{ Configuration, Environment }
class CircuitBreakerModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
bind[CircuitBreakersPanel].to[CircuitBreakersPanelImpl],
// for backward compatibility we still need to provide it for wiring
bind[CircuitBreakers].toProvider[CircuitBreakersProvider],
bind[CircuitBreakerMetricsProvider].toProvider[CircuitBreakerMetricsProviderProvider]
)
}
}
@Singleton
class CircuitBreakersProvider @Inject() (
system: ActorSystem,
circuitBreakerConfig: CircuitBreakerConfig,
metricsProvider: CircuitBreakerMetricsProvider
) extends Provider[CircuitBreakers] {
lazy val get = {
// only in case some 3rd party lib are still wiring the old one.
system.log.warning(
"CircuitBreakers is deprecated, use CircuitBreakersPanel instead. This warning is probably caused by your " +
"service locator. If you are using a 3rd party service locator, upgrade your dependencies, otherwise this " +
"service locator could become incompatible with Lagom in future versions."
)
new CircuitBreakers(system, circuitBreakerConfig, metricsProvider)
}
}
示例10: I18nModule
//设置package包名称以及导入依赖的类
package modules
import play.api.i18n.{DefaultLangs, Langs, MessagesApi}
import play.api.{Configuration, Environment}
import play.api.inject.{Binding, Module}
class I18nModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
bind[Langs].to[DefaultLangs],
bind[MessagesApi].to[services.MyMessagesApi],
bind[play.i18n.MessagesApi].toSelf,
bind[play.i18n.Langs].toSelf
)
}
}
示例11: ServiceLocatorModule
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.dns
import com.lightbend.lagom.javadsl.api.ServiceLocator
import play.api.{ Configuration, Environment, Mode }
import play.api.inject.{ Binding, Module }
import javax.inject.Singleton
import play.api.libs.concurrent.Akka
import com.lightbend.dns.locator.{ ServiceLocator => ServiceLocatorService }
class ServiceLocatorModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] =
if (environment.mode == Mode.Prod)
Seq(
bind[ServiceLocator].to[DnsServiceLocator].in[Singleton],
Akka.bindingOf[ServiceLocatorService]("ServiceLocatorService")
)
else
Seq.empty
}