简体   繁体   English

在playframework中设置像这样的GLOBAL变量是一种好习惯吗?

[英]Is it a good practice to set GLOBAL variables like this in playframework?

I saw codes like this in app/Global.scala , which is written by my colleague 我在我的同事写的app/Global.scala看到了这样的代码

import java.lang.reflect.Constructor
import securesocial.core.RuntimeEnvironment
import securesocial.core.providers._
import securesocial.core.providers.utils.Mailer
import services.UserService

import scala.collection.immutable.ListMap

object Global extends play.api.GlobalSettings {

  /**
   * The runtime environment for this app.
   */
  object MyRuntimeEnvironment extends RuntimeEnvironment.Default[User] {
    // override lazy val routes = new CustomRoutesService()
    override lazy val userService: UserService = new UserService()
    lazy val MailTemplates: MailTemplates = new MailTemplates(this)
    override lazy val mailer: Mailer = new Mailer(MailTemplates)
    // override lazy val eventListeners = List(new MyEventListener())
    override lazy val providers = ListMap(
      // oauth 2 client providers
      include(new FacebookProvider(routes, cacheService, oauth2ClientFor(FacebookProvider.Facebook))),
      // username password
      include(new UsernamePasswordProvider(userService, avatarService, passwordHashers))
    )
  }

  /**
   * An implementation that checks if the controller expects a RuntimeEnvironment and
   * passes the instance to it if required.
   *
   * This can be replaced by any DI framework to inject it differently.
   *
   * @param controllerClass
   * @tparam A
   * @return
   */
  override def getControllerInstance[A](controllerClass: Class[A]): A = {
    val instance = controllerClass.getConstructors.find { c =>
      val params = c.getParameterTypes
      params.length == 1 && params(0) == classOf[RuntimeEnvironment[User]]
    }.map {
      _.asInstanceOf[Constructor[A]].newInstance(MyRuntimeEnvironment)
    }
    instance.getOrElse(super.getControllerInstance(controllerClass))
  }
}

Does anyone think it is suitable to set services as global values in playframework? 有人认为在Playframework中将服务设置为全局值是否合适?

Services are usually implemented as stateless singletons so I would say that it is ok. 服务通常实现为无状态单例,因此我可以说是可以的。 Your question is more related to general dependency injection principles than to Play. 您的问题与一般依赖性注入原则有关,而不是与Play有关。

Yes. 是。 The is a standard dependency injection pattern for play in the absence of using a dependency injection framework. 这是在没有使用依赖项注入框架的情况下进行播放的标准依赖项注入模式。 It allows you do request scoped variable similar to Spring request scope. 它允许您执行与Spring请求范围类似的请求范围变量。

Your implementation looks like its based on the SecureSocial sample implementation in play. 您的实现看起来像是基于正在发挥作用的SecureSocial示例实现。 See https://github.com/jaliss/securesocial/blob/master/samples/scala/demo/app/Global.scala 参见https://github.com/jaliss/securesocial/blob/master/samples/scala/demo/app/Global.scala

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

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