You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Play Framework添加新控制器时出现CreationException问题求助

问题描述

在Play Framework中添加新控制器时遇到以下异常:

CreationException: Unable to create injector, see the following errors:

1) No implementation for service.GithubService was bound.
  while locating service.GithubService
    for the 2nd parameter of controllers.GithubController.<init>(GithubController.scala:12)
  while locating controllers.GithubController
    for the 3rd parameter of router.Routes.<init>(Routes.scala:28)
  at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:139):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)

1 error

相关代码文件如下:

HomeController.scala

package controllers

import play.api._
import play.api.mvc.{BaseController, ControllerComponents}

trait HomeController extends BaseController {
  protected def controllerComponents: ControllerComponents
}

GithubController.scala

package controllers

import javax.inject.Inject
import models.GithubRepositoryDTO
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import service._

import scala.concurrent.{ExecutionContext, Future}

class GithubController @Inject()(
                                  val controllerComponents: ControllerComponents,
                                  githubService: GithubService
                                )(implicit ec: ExecutionContext) extends HomeController {
  def getStarred(): Action[JsValue] = Action.async(parse.json) { implicit request =>
      githubService.getStarredRepositories()

      Future(Ok("test"))
  }
}

GithubService.scala

package service

import models.GithubRepositoryDTO

import scala.concurrent.Future

trait GithubService {
  def getStarredRepositories():Future[Seq[GithubRepositoryDTO]]
}

GithubServiceImpl.scala

package service

import javax.inject.Inject
import models.GithubRepositoryDTO
import play.api.libs.json._
import play.api.libs.ws.WSClient

import scala.concurrent.{ExecutionContext, Future}

class GithubServiceImpl @Inject()(
                                  wsClient:WSClient
)(implicit ec: ExecutionContext) extends GithubService {
  def getStarredRepositories(): Future[Seq[GithubRepositoryDTO]] = {
    //some code
  }
}

错误原因

Play Framework使用Guice作为依赖注入容器,当前仅定义了GithubService接口和它的实现类GithubServiceImpl,但Guice未收到明确的绑定指令,无法确定注入GithubService时应该使用哪个实现类。

解决方案

有两种常用方式解决该绑定问题:

方法1:使用@ImplementedBy注解

直接在GithubService接口上添加@ImplementedBy注解,指定对应的实现类:

package service

import com.google.inject.ImplementedBy
import models.GithubRepositoryDTO

import scala.concurrent.Future

@ImplementedBy(classOf[GithubServiceImpl])
trait GithubService {
  def getStarredRepositories():Future[Seq[GithubRepositoryDTO]]
}

添加后Guice会自动完成接口与实现类的绑定,无需额外配置。

方法2:创建自定义Guice模块

如果需要更灵活的绑定规则(比如不同环境使用不同实现),可创建自定义模块:

  1. 在modules包下创建Module.scala文件:
package modules

import com.google.inject.AbstractModule
import service.{GithubService, GithubServiceImpl}

class Module extends AbstractModule {
  override def configure(): Unit = {
    bind(classOf[GithubService]).to(classOf[GithubServiceImpl])
  }
}
  1. 在conf/application.conf中注册该模块:
play.modules.enabled += "modules.Module"

验证

完成上述任一配置后,重启Play应用,依赖注入容器即可正确解析GithubService的实现,异常将被修复。

内容的提问来源于stack exchange,提问作者Santiago Saavedra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:18:27