如何在play framework2.5的特征中正确注入DBApi? [英] How to inject correctly the DBApi inside a trait in play framework2.5?

查看:47
本文介绍了如何在play framework2.5的特征中正确注入DBApi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在升级到play 2.5框架,并且正如play所提到的,以下代码需要带有依赖项注入

I am upgrading to play framework 2.5 and as mentioned by play the following code needs to be with dependency injection

trait DataSource {
  import play.api.Play.current
  def dataSource = DB.getDataSource()}

是否可以使用具有特征的DI?

is it possible to use DI with trait?

推荐答案

否和是;).

否,因为您至少需要一个实现.

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 

使用@Inject()(...),您可以注入所需的任何内容,例如覆盖trait中的内容.

With @Inject()(...) you can inject whatever you want and for example override stuff from the trait.

这篇关于如何在play framework2.5的特征中正确注入DBApi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆