当前位置: 首页 > 知识库问答 >
问题:

玩框架没有依赖注入?

詹高畅
2023-03-14

在不讨论原因的情况下,假设有人想要一个老式的Play Framework web服务,并且不想使用依赖注入,也不想依赖Google的Guice。在玩法2.8.x中还可能吗?

package controllers
import javax.inject._
import play.api.mvc._
class HomeController @Inject() (val controllerComponents: ControllerComponents) extends BaseController {
  def index = Action {
    Ok("It works!")
  }
}
package controllers
import play.api.mvc.{Action, AnyContent, Controller}
object TestController {
  def index:Action[AnyContent] = Action {
    Ok("It used to work.")
  }
}
[info] Compiling 1 Scala source to /Volumes/.../play-scala-seed/target/scala-2.13/classes ...
[error] p.a.h.DefaultHttpErrorHandler - 

! @7ef69nl6l - Internal server error, for (GET) [/test/] ->

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) Could not find a suitable constructor in controllers.TestController. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at controllers.TestController.class(TestController.scala:3)
  while locating controllers.TestController
    for the 4th parameter of router.Routes.<init>(Routes.scala:33)
  at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:137):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)

1 error]
    at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:210)
    at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:141)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:296)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:186)
    at akka.stream.impl.fusing.MapAsync$$anon$30.onPush(Ops.scala:1261)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:541)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:423)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:624)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:501)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:599)
Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:

1) Could not find a suitable constructor in controllers.TestController. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at controllers.TestController.class(TestController.scala:3)
  while locating controllers.TestController
    for the 4th parameter of router.Routes.<init>(Routes.scala:33)
  at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:137):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:543)
    at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:159)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
    at com.google.inject.Guice.createInjector(Guice.java:87)
    at com.google.inject.Guice.createInjector(Guice.java:78)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:200)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:155)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
    at play.core.server.DevServerStart$$anon$1.$anonfun$reload$3(DevServerStart.scala:189)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)

有没有一个简单的方法可以让你不去这里就呆在老学校里?

我承认,但不完全理解https://www.playframework.com/documentation/2.4.x/migration24。我认为我的问题与2.7中删除了静态路由有关。

共有1个答案

司马晋
2023-03-14

我的名声不允许我对Mario Galic的答案发表评论,但您可以使用BuiltinComponentsFromContext提供的“正确”(非测试)ControllerComponents来轻松修改他的示例。

整个示例看起来像

class HomeController(override protected val controllerComponents: ControllerComponents)
  extends BaseController {
  def index = Action { Ok("It works!") }
}

class MyApplicationLoader extends ApplicationLoader {
  def load(context: ApplicationLoader.Context): Application = {
    new BuiltInComponentsFromContext(context) {
      lazy val homeController = HomeController(controllerComponents)
      override lazy val router: Router = Routes(httpErrorHandler, homeController)
    }.application
  }
}
 类似资料:
  • 问题内容: 我想知道 _什么是Spring Framework? 为什么和何时应该在Java Enterprise开发中使用它? _ 答案将是“依赖注入框架”。好了,使用依赖注入框架时我们有什么优势?用setter值和/或构造函数参数描述类的想法对我来说似乎很奇怪。为什么这样 因为我们可以更改属性而无需重新编译项目?这就是我们所获得的一切吗? 那么,我们应该用什么对象来描述?所有对象还是只有几个?

  • 我已经开始涉足ASP. Net Core,并发现依赖注入是ASP. Net Core框架中的一流公民,它是内置的,可以用于注入各种服务和库。 我想知道他们正在使用哪个依赖注入框架。他们的文档介绍了ASP中的依赖项注入。网芯 ASP。NET Core从一开始就被设计为支持和利用依赖注入。ASP。NET核心应用程序可以通过将内置框架服务注入Startup类中的方法来利用这些服务,应用程序服务也可以配置

  • 从戏剧!框架文档: Play2.0中没有内置的JPA实现;您可以选择任何可用的实现。例如,要使用Hibernate,只需将依赖项添加到项目中: 我的选择是什么而不是冬眠? 你认为什么最适合留言板网站? 我知道Hibernate有一点开销,对吗?

  • 我有一个类,它扩展了 创建表主题的第一条记录可以,但无法创建另一条记录: [PersistenceException:执行DML bindLog时出错[]错误[ERROR:重复的键值违反了唯一约束“pk\U主题”\n详细信息:键(id)=(1)已存在。]] 例外情况当然发生在这一行: 这很奇怪,因为: 它以前工作没有问题 我应该怎么做才能解决这个问题? 类(当然不是全部,只是相关的):

  • 本文向大家介绍ABP框架的基础配置及依赖注入讲解,包括了ABP框架的基础配置及依赖注入讲解的使用技巧和注意事项,需要的朋友参考一下 配置ABP 配置是通过在自己模块的PreInitialize方法中来实现的 代码示例如下: 和orchard类似,abp框架一开始就被设计成模块化的,不同的模块可以通过abp框架来进行配置。举个例子吧,不同的模块都可以添加导航,通过导航添加菜单项到自己定义的主菜单,具