简体   繁体   中英

Play framework 2.4, Guice, and securesocial

I am attempting to follow a tutorial that I found at http://www.filtercode.com/play/play-scala-securesocial . But the tutorial was for Play 2.3. I am having issues because in play version 2.4 they removed getControllerInstance from play.api.GlobalSettings. How would I use Guice dependency injection to accomplish the same functionality desired?

    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))
      }

    }

Error Message if I remove the Cake Pattern snippet above:

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

Right now SecureSocial doesn't support dependency injection (JSR 330) and on version 2.4, Play team did a major change towards elimination of global state, using DI for that task.

Keep an eye on issue 556

UPDATE: as pointed by Jorge since version 3.0-M4, Play 2.4 is supported.

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

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