scala - Play with @ 中的依赖注入(inject) Controller

标签 scala intellij-idea dependency-injection playframework-2.4

这不是阻塞问题,但我想了解 Play 如何更好地处理 Controller 注入(inject)。

Play document about Routing in 2.4 ,据说:

Play supports generating two types of routers, one is a dependency injected router, the other is a static router. The default is the static router, but if you created a new Play application using the Play seed Activator templates, your project will include the following configuration in build.sbt telling it to use the injected router:

routesGenerator := InjectedRoutesGenerator

The code samples in Play’s documentation assumes that you are using the injected routes generator. If you are not using this, you can trivially adapt the code samples for the static routes generator, either by prefixing the controller invocation part of the route with an @ symbol, or by declaring each of your controllers as an object rather than a class.

因此,据我了解,在 build.sbt 中使用 routesGenerator := InjectedRoutesGenerator ,如果我使用 @ 前缀进行 Controller 调用,它与将 Controller 作为单例对象相同。这违背了我已经将 Controller 声明为具有一些注入(inject)的 class 的事实(使用 @Inject),并且我在所有 Controller 调用前都添加了 @ 前缀 这样IntelliJ就不会报错。

那么,@ 真正对 Controller 调用起什么作用?

最佳答案

@StaticRoutesGenerator 的一个选项,它与 Injected 无关。

当您按照推荐的方式使用 InjectedRoutesGenerator 时,您的 Controller 必须是一个类,将在其中注入(inject)依赖项(如果使用运行时 DI,则自动注入(inject);如果使用运行时 DI,则手动注入(inject))您使用编译时 DI)。在路由文件中,您只需引用不带 @ 的类。

如果您选择StaticRoutesGenerator,路由器将遵循与 Play 2.3 及更低版本相同的行为。 Controller 默认是对象,您可以添加@来引用类。

我希望这能解决您的担忧。

关于scala - Play with @ 中的依赖注入(inject) Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32211411/

相关文章:

scala - 使用尾递归和匹配表达式对 Scala 中的选项列表求和

scala - Shapeless:迭代联积中的类型

scala - Akka Streams 按单个源累积

intellij-idea - 如何使用实时模板将 $SELECTION$ 转换为蛇形案例(例如)?

java - Intellij IDEA 调试器不适用于 Gradle Vert.X 项目

c# - ASP .Net Core 中静态方法的依赖注入(inject)

javascript - 如何将商店注入(inject)面板的工具栏中?

scala - 为什么 Scala 会在文件中看到更多行?

intellij-idea - 如何在 IntelliJ 中禁用谷歌快捷方式中的搜索?

c# - 具有依赖注入(inject)的 Entity Framework Core 是如何处理的?