当前位置: 首页>>代码示例>>Scala>>正文


Scala Module类代码示例

本文整理汇总了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
  }
} 
开发者ID:oen9,项目名称:bard-api,代码行数:34,代码来源:CustomObjectMapperModule.scala

示例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]]
} 
开发者ID:callidustaurus,项目名称:github-api,代码行数:24,代码来源:PullRequestClientModule.scala

示例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
    )
  }
} 
开发者ID:bananapianist,项目名称:playfw_sample,代码行数:26,代码来源:MailsendModule.scala

示例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
    )
  }
} 
开发者ID:bananapianist,项目名称:playfw_sample,代码行数:26,代码来源:HelloWorldModule.scala

示例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
    )
  }
} 
开发者ID:bananapianist,项目名称:playfw_sample,代码行数:28,代码来源:CustomerCountBatchModule.scala

示例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()
} 
开发者ID:wouzar,项目名称:tracker,代码行数:41,代码来源:MyApplicationFactory.scala

示例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)
  }
} 
开发者ID:jboner,项目名称:lagom-service-locator-consul,代码行数:30,代码来源:ConsulServiceLocatorModule.scala

示例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()
  }
} 
开发者ID:21re,项目名称:play-micro-tools,代码行数:62,代码来源:ConfigurationBindings.scala

示例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])

    )
  }
} 
开发者ID:WeltN24-public,项目名称:WeltContentApiClient,代码行数:26,代码来源:PlayModule.scala

示例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])

    )
  }
} 
开发者ID:WeltN24-public,项目名称:WeltContentApiClient,代码行数:26,代码来源:PlayModule.scala

示例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

  }
} 
开发者ID:psycho-ir,项目名称:cachecool,代码行数:53,代码来源:CachecoolModule.scala

示例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()
  }
} 
开发者ID:agiledigital,项目名称:kamon-play-extensions,代码行数:36,代码来源:KamonPlayExtensionsModule.scala

示例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)
  }
} 
开发者ID:lagom,项目名称:lagom,代码行数:42,代码来源:CircuitBreakerModule.scala

示例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
    )
  }
} 
开发者ID:kigichang,项目名称:play-gettext,代码行数:18,代码来源:I18nModule.scala

示例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
} 
开发者ID:typesafehub,项目名称:service-locator-dns,代码行数:23,代码来源:ServiceLocatorModule.scala


注:本文中的play.api.inject.Module类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。