简体   繁体   中英

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

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

is it possible to use DI with trait?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM