【发布时间】:2016-12-03 03:14:19
【问题描述】:
我正在使用 Scala 编写 Play 2.5 应用程序。我有以下代码:
@ImplementedBy(classOf[BarRepositoryImpl])
trait BarRepository {
def bar = //some actions
}
class BarRepositoryImpl extends BarRepository
case class Foo( /*some fields*/) {
@Inject private var barRepository: BarRepository = null
def foo1 = {
val a = barRepository.bar //here barRepository is always null
// some actions with 'a' and returning some result which depends on 'a'
}
}
我还有一个控制器,我在其中也注入了 BarRepository,但是通过构造函数并且在 Foo 类中的 val a = barRepository.bar 行中,我得到了 NullPointerException。有人可以帮助找出问题所在吗?案例类中是否禁止使用注入?
【问题讨论】:
标签: scala playframework dependency-injection guice