简体   繁体   中英

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

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?

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.

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.

Your implementation looks like its based on the SecureSocial sample implementation in play. See https://github.com/jaliss/securesocial/blob/master/samples/scala/demo/app/Global.scala

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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