带有依赖注入的Play Framework PathBindable [英] Play Framework PathBindable with Dependency Injection

查看:83
本文介绍了带有依赖注入的Play Framework PathBindable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Scala Play应用程序迁移到2.5,目前正在将我的组件移至依赖项注入.不过,还有一个地方让我茫然不知所措.我在伴随对象中定义了一个PathBindable隐式转换:

I'm migrating a Scala Play application to 2.5 and am currently moving my components to dependency injection. There's one place left where I'm at a loss how to do it though. I have a PathBindable implicit conversion defined in the companion object:

object Task {
  implicit def pathBindable(implicit stringBinder: PathBindable[String]) =
    new PathBindable[Task] {
       ...
    }
}

PathBindable的实现需要从存储库中查找对象,但是我还没有找到一种在此处依赖注入存储库的方法.解决方法是,使用现在不建议使用的Play对象:

The implementation of the PathBindable needs to look up the object from a repository, but I haven't found a way to dependency-inject the repository here. As a workaround I'm using the now deprecated Play object:

val tasks = Play.application(Play.current).injector.instanceOf[TasksRepository]

任何想法如何正确解决此问题?

Any ideas how to solve this properly?

推荐答案

在IO线程上运行,因此应该快速而不阻塞.

According to Lightbend Engineer Greg Methvin, PathBindables should only depend on the state in the path. The reason is that the code runs on the IO thread and should therefore be fast and not block.

这篇关于带有依赖注入的Play Framework PathBindable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆