我有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
答案 0 :(得分:1)
您可以尝试将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
}