本文整理汇总了Scala中play.api.inject.Module类的典型用法代码示例。如果您正苦于以下问题:Scala Module类的具体用法?Scala Module怎么用?Scala Module使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Module类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CustomObjectMapperModule
//设置package包名称以及导入依赖的类
package modules
import javax.inject.{Inject, Provider, Singleton}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import play.api.inject.{ApplicationLifecycle, Module}
import play.api.{Configuration, Environment}
import play.libs.Json
import scala.concurrent.Future
class CustomObjectMapperModule extends Module {
def bindings(environment: Environment, configuration: Configuration) = Seq(
bind[ObjectMapper].toProvider[ObjectMapperProvider].eagerly()
)
}
@Singleton
class ObjectMapperProvider @Inject() (lifecycle: ApplicationLifecycle) extends Provider[ObjectMapper] {
lazy val get : ObjectMapper = {
val objectMapper = Json.newDefaultMapper()
objectMapper.registerModule(DefaultScalaModule)
Json.setObjectMapper(objectMapper)
lifecycle.addStopHook { () =>
Future.successful(Json.setObjectMapper(null))
}
objectMapper
}
}
示例2: 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]]
}
示例3: 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
)
}
}
示例4: 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
)
}
}
示例5: CustomerCountBatchActorModule
//设置package包名称以及导入依赖的类
package modules
import play.api.{Configuration, Environment}
import play.api.inject.{Binding, Module}
import play.api.libs.concurrent.AkkaGuiceSupport
import actors.CustomerCountBatchActor
import com.google.inject.AbstractModule
import schedulers.CustomerCountBatchScheduler
import akka.actor.ActorSystem
class CustomerCountBatchActorModule extends AbstractModule with AkkaGuiceSupport {
override def configure() = {
bindActor[CustomerCountBatchActor]("CustomerCountBatch-actor")
}
}
class CustomerCountBatchScheduleModule extends Module {
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = {
Seq(
bind[CustomerCountBatchScheduler].toSelf.eagerly
)
}
}
示例6: 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()
}
示例7: 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)
}
}
示例8: 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()
}
}
示例9: PlayModule
//设置package包名称以及导入依赖的类
package de.welt.contentapi.client
import de.welt.contentapi.client.services.configuration.{ContentClientConfig, ContentClientConfigImpl, SectionConfigurationService, SectionConfigurationServiceImpl}
import de.welt.contentapi.client.services.contentapi._
import de.welt.contentapi.client.services.s3.{S3, S3Impl}
import play.api.inject.Module
import play.api.{Configuration, Environment}
class PlayModule extends Module {
override def bindings(environment: Environment, configuration: Configuration) = {
Seq(
bind(classOf[ContentClientConfig]).to(classOf[ContentClientConfigImpl]).eagerly,
bind(classOf[ContentService]).to(classOf[ContentServiceImpl]).eagerly,
bind(classOf[S3]).to(classOf[S3Impl]),
bind(classOf[ContentEnrichmentService]).to(classOf[ContentEnrichmentServiceImpl]),
bind(classOf[SectionConfigurationService]).to(classOf[SectionConfigurationServiceImpl]),
bind(classOf[LegacySectionService]).to(classOf[LegacySectionServiceImpl]),
bind(classOf[ContentSearchService]).to(classOf[ContentSearchServiceImpl])
)
}
}
示例10: PlayModule
//设置package包名称以及导入依赖的类
package de.welt
import de.welt.services.configuration.{ContentClientConfig, ContentClientConfigImpl, SectionConfigurationService, SectionConfigurationServiceImpl}
import services.contentapi.{LegacySectionService, LegacySectionServiceImpl, _}
import de.welt.services.s3.{S3, S3Impl}
import play.api.inject.Module
import play.api.{Configuration, Environment}
class PlayModule extends Module {
override def bindings(environment: Environment, configuration: Configuration) = {
Seq(
bind(classOf[ContentClientConfig]).to(classOf[ContentClientConfigImpl]).eagerly,
bind(classOf[ContentService]).to(classOf[ContentServiceImpl]).eagerly,
bind(classOf[S3]).to(classOf[S3Impl]),
bind(classOf[ContentEnrichmentService]).to(classOf[ContentEnrichmentServiceImpl]),
bind(classOf[SectionConfigurationService]).to(classOf[SectionConfigurationServiceImpl]),
bind(classOf[LegacySectionService]).to(classOf[LegacySectionServiceImpl]),
bind(classOf[ContentSearchService]).to(classOf[ContentSearchServiceImpl])
)
}
}
示例11: 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
}
}
示例12: 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()
}
}
示例13: 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)
}
}
示例14: 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
)
}
}
示例15: 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
}