我试图在Play的文档中关注this example来测试带有注入对象的Controller。我复制了这个例子,但是在尝试调用网页时遇到了错误:
没有绑定test.Component的实现。
错误似乎正确,因为我没有调用binding
方法,但如何解决这个问题?
这是我的代码:
package test
import play.api.mvc._
import javax.inject.Inject
import play.api.{ Environment, Configuration }
import play.api.inject.Module
trait Component {
def hello: String
}
class DefaultComponent extends Component {
def hello = "default"
}
class MockComponent extends Component {
def hello = "mock"
}
class ComponentModule extends Module {
def bindings(env: Environment, conf: Configuration) = Seq(
bind[Component].to[DefaultComponent]
)
}
class Application @Inject() (component: Component) extends Controller {
def index() = Action {
Ok(component.hello)
}
}
答案 0 :(得分:0)
添加到application.conf
play {
modules {
enabled += test.ComponentModule
}
}