玩框架2.4,Guice和securesocial

时间:2015-06-02 00:15:44

标签: scala playframework dependency-injection securesocial

我正在尝试按照我找到的教程进行操作 http://www.filtercode.com/play/play-scala-securesocial。但该教程适用于Play 2.3。我遇到了问题,因为在播放版本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))
      }

    }

如果我删除上面的Cake Pattern代码段错误消息:

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

2 个答案:

答案 0 :(得分:2)

现在SecureSocial不支持依赖注入(JSR 330),而在版本2.4上,Play团队在消除全局状态方面做了重大改变,使用DI执行该任务。

密切关注问题556

更新:正如Jorge从版本3.0-M4开始所指出的那样,支持Play 2.4。

答案 1 :(得分:2)

启动3.0-M4,您可以使用Play 2.4