在 playframework 中像这样设置 GLOBAL 变量是一种好习惯吗?
Is it a good practice to set GLOBAL variables like this in playframework?
我在同事写的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))
}
}
有没有人认为在 playframework 中将服务设置为全局值合适?
服务通常作为无状态单例实现,所以我认为这没问题。您的问题与一般依赖注入原则相关,而不是与 Play 相关。
是的。这是在没有使用依赖注入框架的情况下播放的标准依赖注入模式。它允许您请求范围变量类似于 Spring 请求范围。
您的实施看起来像是基于正在运行的 SecureSocial 示例实施。看
https://github.com/jaliss/securesocial/blob/master/samples/scala/demo/app/Global.scala
我在同事写的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))
}
}
有没有人认为在 playframework 中将服务设置为全局值合适?
服务通常作为无状态单例实现,所以我认为这没问题。您的问题与一般依赖注入原则相关,而不是与 Play 相关。
是的。这是在没有使用依赖注入框架的情况下播放的标准依赖注入模式。它允许您请求范围变量类似于 Spring 请求范围。
您的实施看起来像是基于正在运行的 SecureSocial 示例实施。看 https://github.com/jaliss/securesocial/blob/master/samples/scala/demo/app/Global.scala