Play Framework: Dependency Inject Action Builder

后端 未结 3 1906
猫巷女王i
猫巷女王i 2020-12-14 19:30

since Play Framework 2.4 there is the possibility to use dependency injection (with Guice).

Before I used objects (for example AuthenticationService) in

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 20:20

    I like accepted answer but for some reason the compiler would not recognize the authService reference. I got around this pretty easily by just sending the service in the method signature, a la...

    class Authentication @Inject()(authenticationService: AuthenticationService) extends Controller with ActionBuilders {
    
      def testAuth = AuthenticatedAction(authenticationService).async { implicit request =>
        Future.successful(Ok("Authenticated!"))
      }
    
    }
    

提交回复
热议问题