播放框架交互式控制台 - 具有 DI 的访问类



我使用Play 2.6。

我使用sbt console进入交互式控制台。

我使用此链接启动应用程序 https://www.playframework.com/documentation/2.6.x/PlayConsole#launch-the-interactive-console

我的典型模型

如下
@Singleton
class UserRepository @Inject() (@NamedDatabase("mysql") dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {

如何访问 REPL 中用户存储库的单例实例?

我使用此链接引导应用程序 https://www.playframework.com/documentation/2.6.x/PlayConsole#launch-the-interactive-console

这将使您能够访问该应用程序

import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.createContext(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
// Now to load my classwith all the dependencies injected and ready to use
val c = Class.forName("user.UserRepository")
val repo = app.injector.instanceOf(c).asInstanceOf[user.UserRepository]
repo.list

最新更新