0

In Play 2.5.X we need to use dependency injection. The following code is an example from play-documentation:

class HomeController @Inject() (configuration: play.api.Configuration) extends Controller {
  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }
}

If I define some additional helper method getXXX in HomeController and need to access it from somewhere else, how can I access this method?

What I am really interested in is how to create instance of HomeController so that I can use homeControllerInstance.getXXX.

If I try to create instance like following:

val homeControllerInstance = new HomeController()

I get error:

Compilation error[not enough arguments for constructor HomeController: (configuration: play.api.Configuration)HomeController

I tried to change HomeController class declaration to:

class HomeController @Inject(configuration: play.api.Configuration) extends Controller

But this gave me error:

classfile annotation arguments have to be supplied as named arguments

Can someone please explain, how can I fix this?

marcospereira
  • 12,045
  • 3
  • 46
  • 52
Shailesh
  • 358
  • 2
  • 13
  • What is this helper method? Are you sure that controller is the best place to keep it? – michaJlS Oct 14 '16 at 11:21
  • Your question is not very clear. Are you asking how to declare the Controller as an injectable component? – jacks Oct 14 '16 at 11:26
  • Is this for unit testing ? If this is the case, you might want to check out the guides https://www.playframework.com/documentation/2.5.x/ScalaTestingWithGuice – AME Oct 14 '16 at 11:28
  • after you marked you controller with @Inject annotation all parameters will be injected to HomeController and you can then inject HomeController itself whenever you need. – iuriisusuk Oct 14 '16 at 12:25
  • @michaJlS: This helper method creates HTTP request and send it to some URL. URL is read from application.conf. – Shailesh Oct 14 '16 at 12:32
  • @Nio: No, I am not asking "how to declare the Controller as an injectable component?" – Shailesh Oct 14 '16 at 12:32
  • 1
    I think you probably want to put this re-useable helper method in a service and then you can inject that in your controllers . In my opinion that would be the most reusable and you can keep your controllers slim/focused on what they do and delegate interaction with some external thing to another component – Barry Oct 14 '16 at 13:26

3 Answers3

0

If you want to use your getXXX method of your class HomeController from Xyz class using google juice, try this

import com.google.inject.Inject

class Xyz @Inject()(homeCon: HomeController) {
   homeCon.getXXX
}

Although I am not sure what your actual problem is, but hope this helps.

Arpit Suthar
  • 754
  • 1
  • 5
  • 19
0

You should instantiate the class with

play.api.Play.current.injector.instanceOf(classOf[UID])
searchrome
  • 329
  • 2
  • 5
0

Sometimes you need to put a new line after the @Inject keyword so this:

class HomeController @Inject(a :IWantThisInjected) (configuration: play.api.Configuration)

fails with that error, but moving the first set of params to the next line can fix it:

class HomeController @Inject (a :IWantThisInjected) (configuration: play.api.Configuration)

Philluminati
  • 2,649
  • 2
  • 25
  • 32