简体   繁体   中英

Managed controllers in Play 2.3 with DI (Scaladi / Guice)

thank you in advance for taking the time to take a look at my current problem with play.

I am still trying out the Playframework, currently in version 2.3.

At the moment I try to figure dependency injection out. I followed the tutorials provided in the activator for guice as well as scaladi and always resulting in the same compiler error:

play.PlayExceptions$CompilationException: Compilation error[object Application is not a member of package controllers Note: class Application exists, but it has no companion object.]

This makes me think I am missing a more universal part of the Playframework because the problem is not tied to a specific DI framework.

So let me describe what I tried with scaladi (any working solution with guice would also be appreciated):

defining the route in conf/routes:

GET     /        @controllers.Application.index

(added the @ for support of managed controllers which is supported since play 2.1 afaik)

defining the controller:

package controllers

import scaldi.{Injector, Injectable}
import scala._
import play.api._
import play.api.mvc._
import services.GreetingService

class Application(implicit inj: Injector) extends Controller with Injectable {
  val greetService= inject [GreetingService]

  def index = Action {
    Ok("Here I am")
  }
}

defining the Global for modifing the GlobalSetting under app\\Global.scala:

import modules.ApplicationModule
import play.api.GlobalSettings
import scaldi.play.ScaldiSupport

object Global extends GlobalSettings with ScaldiSupport {

  def applicationModule = new ApplicationModule
}

and finally the application module under app\\modules\\ApplicationModule.scala:

package modules

import controllers.Application
import scaldi.Module

class ApplicationModule extends Module {
  binding to new Application
}

So I really would appreciate any help to figure out why Application class could not be located within the package controllers. Also again I would be happy about any working solution may it be guice or scaladi.

Thank you!

Edit: The problem was a second controller for a different route that was also registered under "/conf/routes/". This controller was not yet manged. So after I adapted the steps I described above to the second Controller everything worked fine.

So it seems the mistake was to have another route configured which was not managed. This caused the object Application is not a member of package controllers Note: class Application exists, but it has no companion object. error.

Because my other controller (User) was indeed an object and not a class. So transfering the UserController to class and scaldi fixed this issue. The transfer of the controller was identical to the Applicationcontroller i described above.

Controller instances (declared in a routing using the @ notation) are initialised using a method in Global :

def getControllerInstance[A](controllerClass: Class[A]): A

So if you want to plug-in a DI framework, you need to override this method to turn a Class[A] into an instance of A .

就我而言,我通过在build.sbt中添加以下内容来解决:

routesGenerator := InjectedRoutesGenerator

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