2017-07-04 4 views
1

でアノテートさのどちらか1つだけのコンストラクタを持っている必要があります私はPlayFrameworkでGuiceのを使用していますが、私はランタイムエラーを取得しています:ここでProvisionException:クラスが@Injectまたはゼロ引数

Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors: 

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

1 error 
    at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025) ~[guice-4.0.jar:na] 
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051) ~[guice-4.0.jar:na] 
    at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:321) ~[play_2.11-2.4.3.jar:2.4.3] 
    at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:316) ~[play_2.11-2.4.3.jar:2.4.3] 
    at play.api.Application$class.routes(Application.scala:111) ~[play_2.11-2.4.3.jar:2.4.3] 

はコードです:私が追加したbuild.sbt

class DirectUserController(var factory:FactoryHandlerTrait) extends Controller { 
    //rest of the code 
} 

trait FactoryHandlerTrait { 
    def getDirectUserFactory: DirectUserFactory 

    def getUserRepository: UserRepository 

    def getUrlRepository: URLRepository 
} 

class FactoryHandler(var s: String = "real") extends FactoryHandlerTrait { 
    def getDirectUserFactory: DirectUserFactory = { 
    //implementation here 
    } 

    def getUserRepository: UserRepository = { 
    //implementation here 
    } 

    def getUrlRepository: URLRepository = { 
    //implementation here 
    } 
} 

class DependencyModule extends Module { 

    def configure(binder: Binder) = { 
    binder.bind(classOf[FactoryHandlerTrait]).to(classOf[FactoryHandler]) 
    } 
} 

そして、ここでは私のコントローラであります:

routesGenerator := InjectedRoutesGenerator 

何が間違っているのですか?その例外を避けるには?

私はPlay 2.4.3とGuice 3.0を使用しています。

答えて

2

guiceによると、コンストラクタには引数がないか、または@Injectで注釈を付ける必要があります。あなたのクラスFactoryHandlerには、これらの要件のいずれも満たされていません。

これを解決するには、パラメータsがguiceによって(おそらくnot)注入できる引数かどうか考えてみてください。したがって、あなたは何とかそれを渡す必要があります。実行時にしか利用できない場合は、補助注入を調べることをお勧めします。私は前の答えでこれがどのように機能するかを説明しました:https://stackoverflow.com/a/35960962/1080523

関連する問題