我有一个Scala特质
trait UserRepository {
def findByEmail(email: String): User
}
我想将其注入到MacWire服务中
class AccountService(){
val userRepo = wire[UserRepository]
}
然后在测试或课程中使用它
class AccountServiceSpec {
val userRepo = new UserRepositoryImpl()
val accountSvc = new AccountService() //<--not manually injecting repo in service constructor
}
但是我在服务类中遇到了一个编译错误
找不到accounts.repository.UserRepository的公共构造函数或伴随对象
您可以尝试转换userRepo
为类参数,该参数允许macwire自动提供其服务价值:
import com.softwaremill.macwire._
case class User(email: String)
trait UserRepository {
def findByEmail(email: String): User
}
class AccountService(val userRepo: UserRepository)
class UserRepositoryImpl extends UserRepository{
def findByEmail(email: String): User = new User(email)
}
class AccountServiceSpec {
val userRepo = new UserRepositoryImpl()
val accountSvc = wire[AccountService] //<--not manually injecting repo in service constructor
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句