[英]How to inject correctly the DBApi inside a trait in play framework2.5?
I am upgrading to play framework 2.5 and as mentioned by play the following code needs to be with dependency injection 我正在升级到播放框架2.5,并且如播放所述,以下代码需要进行依赖项注入
trait DataSource {
import play.api.Play.current
def dataSource = DB.getDataSource()}
is it possible to use DI with trait? 可以将DI与特征一起使用吗?
No and Yes;). 否,是;)。
No because you need at least one Implementation. 否,因为您至少需要一个实现。
If you have one Implementation, the only thing is that it must be a simple Class (not a case class). 如果您有一个实现,那么唯一的事情就是它必须是一个简单的类(而不是案例类)。
class MyDataSource @Inject()(val dataSource: PlayDataSource) extends DataSource
With @Inject()(...)
you can inject whatever you want and for example override stuff from the trait
. 使用@Inject()(...)
您可以注入所需的任何东西,例如覆盖trait
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.