【问题标题】:Play framework 2.4, Guice, and securesocialPlay framework 2.4、Guice 和securesocial
【发布时间】:2015-06-02 00:15:43
【问题描述】:

我正在尝试按照我在 http://www.filtercode.com/play/play-scala-securesocial。但本教程适用于 Play 2.3。我遇到了问题,因为在 play 2.4 版中,他们从 play.api.GlobalSettings 中删除了 getControllerInstance。我将如何使用 Guice 依赖注入来完成所需的相同功能?

    import java.lang.reflect.Constructor
    import securesocial.core.RuntimeEnvironment
    import securesocial.core.providers._
    import securesocial.core.providers.utils.{Mailer, PasswordHasher, PasswordValidator}
    import services.{DemoUserService}
    import models.DemoUser
    import scala.collection.immutable.ListMap

    object Global extends play.api.GlobalSettings {

      /**
       * Demo application's custom Runtime Environment
       */
      object DemoRuntimeEnvironment extends RuntimeEnvironment.Default[DemoUser] {
        override lazy val userService: DemoUserService = new DemoUserService
        override lazy val providers = ListMap(
          include(new FacebookProvider(routes, cacheService, oauth2ClientFor(FacebookProvider.Facebook))),
          include(new GitHubProvider(routes, cacheService, oauth2ClientFor(GitHubProvider.GitHub))),
          include(new GoogleProvider(routes, cacheService, oauth2ClientFor(GoogleProvider.Google))),
          include(new LinkedInProvider(routes, cacheService, oauth1ClientFor(LinkedInProvider.LinkedIn))),
          include(new TwitterProvider(routes, cacheService, oauth1ClientFor(TwitterProvider.Twitter))),
          include(new UsernamePasswordProvider[DemoUser](userService, avatarService, viewTemplates, passwordHashers))
        )
      }

       /**
       * Dependency injection on Controllers using Cake Pattern
       *
       * @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[DemoUser]]
        }.map {
          _.asInstanceOf[Constructor[A]].newInstance(DemoRuntimeEnvironment)
        }
        instance.getOrElse(super.getControllerInstance(controllerClass))
      }

    }

如果我删除上面的蛋糕图案 sn-p 会出现错误消息:

ProvisionException: Unable to provision, see the following errors: 
 1) Could not find a suitable constructor in controllers.Application. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at controllers.Application.class(Application.scala:27) while locating com.google.inject.Provider<controllers.Application> for parameter 1 at router.Routes.<init>(Routes.scala:47) while locating router.Routes while locating play.api.inject.RoutesProvider while locating play.api.routing.Router

 2) Could not find a suitable constructor in securesocial.controllers.LoginPage. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at securesocial.controllers.LoginPage.class(LoginPage.scala:32) while locating com.google.inject.Provider<securesocial.controllers.LoginPage> for parameter 3 at router.Routes.<init>(Routes.scala:47) while locating router.Routes while locating play.api.inject.RoutesProvider while locating play.api.routing.Router

 3) Could not find a suitable constructor in securesocial.controllers.PasswordChange. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at securesocial.controllers.PasswordChange.class(PasswordChange.scala:34) while locating com.google.inject.Provider<securesocial.controllers.PasswordChange> for parameter 5 at router.Routes.<init>(Routes.scala:47) while locating router.Routes while locating play.api.inject.RoutesProvider while locating play.api.routing.Router

 4) Could not find a suitable constructor in securesocial.controllers.ProviderController. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at securesocial.controllers.ProviderController.class(ProviderController.scala:33) while locating com.google.inject.Provider<securesocial.controllers.ProviderController> for parameter 6 at router.Routes.<init>(Routes.scala:47) while locating router.Routes while locating play.api.inject.RoutesProvider while locating play.api.routing.Router

 5) Could not find a suitable constructor in securesocial.controllers.Registration. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at securesocial.controllers.Registration.class(Registration.scala:37) while locating com.google.inject.Provider<securesocial.controllers.Registration> for parameter 4 at router.Routes.<init>(Routes.scala:47) while locating router.Routes while locating play.api.inject.RoutesProvider while locating play.api.routing.Router

 5 errors

【问题讨论】:

    标签: scala playframework dependency-injection securesocial


    【解决方案1】:

    从 3.0-M4 开始你可以使用 Play 2.4

    【讨论】:

      【解决方案2】:

      目前 SecureSocial 不支持依赖注入 (JSR 330),在 2.4 版中,Play 团队对消除全局状态进行了重大更改,使用 DI 来完成该任务。

      关注问题556

      更新:正如 Jorge 所指出的,自 3.0-M4 版起,支持 Play 2.4。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-16
        • 2015-10-08
        • 2016-07-02
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        相关资源
        最近更新 更多