Java 如何在playframework中使用GUI使用提供程序进行依赖项注入
我正在使用PlayFramework2.4这是我的代码Java 如何在playframework中使用GUI使用提供程序进行依赖项注入,java,scala,playframework,guice,playframework-2.4,Java,Scala,Playframework,Guice,Playframework 2.4,我正在使用PlayFramework2.4这是我的代码 trait UserRepositoryTrait { val userRepo:UserRepository val sessionRepo:SessionRepository } class UserRepositoryImpl extends UserRepositoryTrait { @Inject @Named("userRepo")val userRepo:UserRepository= null @Inj
trait UserRepositoryTrait {
val userRepo:UserRepository
val sessionRepo:SessionRepository
}
class UserRepositoryImpl extends UserRepositoryTrait {
@Inject @Named("userRepo")val userRepo:UserRepository= null
@Inject @Named("sessionRepo") val sessionRepo:SessionRepository = null
}
这是模块类
class UserDependencyModule extends AbstractModule {
@Override
protected def configure() {
bind(classOf[UserRepository]).annotatedWith(Names.named("userRepo")).toInstance(new UserRepo)
bind(classOf[SessionRepository]).annotatedWith(Names.named("sessionRepo")).toInstance(new SessionRepo)
bind(classOf[UserRepositoryTrait]).to(classOf[UserRepositoryImpl])
}
}
在application.conf中
play.modules.enabled += "models.guice.UserDependencyModule"
如果我在控制器中注入这个特性,一切都会正常工作,但是我想将这个特性注入一个类,下面是代码
class StatusChange @Inject() (userRepository:UserRepositoryTrait) {
}
我需要在Service.scala
类中调用StatusChange.scala
如何实例化StatusChange.scala
对象
class ArtworkService() {
val status= new StatusChange(//what should i add here?)
}
我确实读过提供商的文章,但我无法理解如何将其用于我的场景?请指导我
更新
如果我这样做会正确吗
class ArtworkService @inject (userRepository: UserRepositoryTrait) {
val status= new StatusChange(userRepository)
}
在控制器中
class MyController @inject (userRepository: UserRepositoryTrait)
{
val artworkService = new ArtworkService(userRepository)
}
您不需要在此处使用提供程序。用于解析循环依赖项引用 在您的情况下,您可以简单地执行以下操作:
class ArtworkService @Inject (status: StatusChange) {}
然后在控制器中直接注入ArtWorksService
:
class MyController @Inject (artworkService: ArtworkService) {}
如果你有一个循环参考,就会出现在图片中。例如:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkService: ArtworkService) {}
这里,我们有cycleArtWorksService
->StatusChange
&StatusChange
->ArtWorksService
,因此,在这些情况下,提供商会出手相救。您可以通过以下方式解决此问题:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkServiceProvider: Provider[ArtworkService]) {}
您的ArtWorksService还需要注入UserRepositoryTrait。那么如何将UserRepositoryTrait参数传递给ArtWorksService类问题仍然存在(该类仅会更改)我需要知道如何传递这些需要@inspected instances的参数,问题是:创建
ArtworkService
的线程的堆栈跟踪的根是什么?换句话说,是什么让代码运行?如果这是一个由某个控制器发起的调用链,那么您可以在整个过程中拥有一个相关@Inject的链(反向,即每次注入将被调用的组件)。如果代码是通过其他方式运行的,那么它是如何运行的?它是由控制器运行的。请再次查看我编辑的问题it@swaheed,仍然不清楚使用类MyController@Inject()(artworkService:artworkService)