I have created a Scala project and I added the following controller in /app/controllers:
package controllers
import play.api._
import play.api.mvc._
class Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
I have added its index method to the routes file:
GET / controllers.Application.index
However, when I access localhost:9000 in the browser, I get the error:
type Application is not a member of package controllers
Why is the controller not visible?
This is likely because your route refers to a static controller, i.e. an object rather than a class. There are a few different ways you could fix this:
Declare your controller as an object:
object Application extends Controller {
...
}
Alternately, you can prefix the routes action reference with a #, indicating that it's a class that should be instantiated when the route is invoked:
GET / #controllers.Application.index
You can also use the injected, rather than the (default) static router, which involves adding the following line to your build.sbt:
routesGenerator := InjectedRoutesGenerator
In the early versions of Play 2.0 controllers were always static objects, but in recent versions it is typical to use a class, into which you can inject components. Have a look at the routes documentation for details.
Related
I have a test specification called MyTestSpec. The aim it is to test a controller that uses dependency injection in its constructor.
class StatusController #Inject()(cc: ControllerComponents, counter: Counter) extends AbstractController(cc) { ...
The Counter class is another controller that provides a status over to the other. When I now try to test this controller, I have the option to either mock the required Counter class, construct the whole chain of controllers / dependencies or inject it. Let's ignore mocking for now, although it probably would be the more correct thing to do.
The issue is that always when I try to inject the Counter controller, I get a NullPointerException.
What I tried so far:
Inject the Counter into the constructor of MyTestSpec, but then MyTestSpec won't even be constructed and tested.
Inject into a local var with the #Inject annotation.
Inject into a val via app.injector.instanceOf[Counter].
Inject via the shortform inject[Counter].
All don't actually give me this other Controller.
Below a rough outline of the test, the new application to get started with Play can be easily adjusted to demonstrate the issue:
class MyTestSpec extends PlaySpec with GuiceOneAppPerTest with Injecting {
"StatusController GET" should {
"render the status page from a new instance of controller" in {
val controller = new StatusController(stubControllerComponents(), counter)
...
}
}
The question: is there a way to simply inject this Counter controller? If there is no simple way, the follow on question would be what components I can then inject anyway if I am already being restrained... ?
Note: this is different to the question asked here, although I tried the solution provided in it.
Note 2: thank you to the Play and ScalaTest developers as well as all these contributors behind the scenes - a wonderful framework.
I'm not sure if there's something really basic that I'm missing, but I can't figure out how to use WSClient. I've seen all of the examples saying you need to pass the WSClient to a class as a dependency, which I've done, but when I run the program what do I actually pass to my class?
For example, my class signature is:
class myClassName(ws: WSClient)
But when I instantiate the class what do I actually pass to it? I'm also happy to ignore the Play! framework stuff if that makes it easier and just use SBT to run it (which I'm more familiar with).
It's unclear where you might be using a WSClient, but it is recommended that you let the Play framework 'manage' the instance of the client. When you instantiate your application, it gets injected:
class Application #Inject() (ws: WSClient) extends Controller {
...
}
What that means is that inside the ... you have access to ws as a value. You can instantiate myClassName using it:
class Application #Inject() (ws: WSClient) extends Controller {
val myclass = myClassName(ws) // passes the injected WSClient to myClassName
}
Or you can write a function that returns the WSClient, so some other area of your code can call into your Application object to get a object handler for it.
But the key is that the Application object gets that handle because of injection, which is the #Inject annotation.
If you need to generate a WSClient and manage it manually, there are good instructions here. The recommended implementation is reliant on Play! framework libraries, but doesn't depend on the Application.
I am using the play framework version 2.49, I am trying to do a dependency injection and it is my first time doing this. I have 3 Folders Interface : Repository : Controllers. The interface lays out abstract methods that I implement inside a repository folder then inject into a controller action. I am only lost when it comes to the controller action. Here is a sample code of mines
Interface
package Interface
abstract class Iprofiles {
def edit_profile
def view_profile
def forgot_password
}
Repository
package Repository
import Interface.Iprofiles
import slick.driver.PostgresDriver.api._
class ProfileRepository extends Iprofiles {
val db= Database.forConfig("database")
// These 3 methods will have Database logic soon
def edit_profile: Unit
def view_profile: Unit
def forgot_password: Unit
}
Controller
package controllers
import play.api.data.Form
import play.api.data.Forms._
class Relations extends Controller {
def MyAction() = Action {
// How can I inject edit_profile in the repository folder here
Ok()
}
}
My Repository methods are empty right now but I will have Data logic in them soon. In My controller MyAction() method for example how can I do a DI and include edit_profile from the repository folder ? I have been searching for how to get this done but nothing has worked
Dependency Injection on the JVM is currently only about injecting object references, not method references. That is, in a class you declare a dependency to a type (typically an interface) and define the mapping from this (interface) type to an implementation (type) in the injector configuration.
During injection -typically during object construction - you get a reference to an object of the implementation type injected.
Then you can call all methods on that injected object.
So you need an attribute taking the reference and this is typically filled via a constructor parameter.
I have been using Play! Framework for Scala for nearly a year now. I am currently using version 2.5.x.
I am aware of the evolution of controllers in Play and how developers have been forced away from static object routes.
I am also aware of the Guice usage in play.
If you download activator and run:
activator new my-test-app play-scala
Activator will produce a template project for you.
My question is specifically around this file of that template.
my-test-app/app/services/Counter.scala
package services
import java.util.concurrent.atomic.AtomicInteger
import javax.inject._
/**
* This trait demonstrates how to create a component that is injected
* into a controller. The trait represents a counter that returns a
* incremented number each time it is called.
*/
trait Counter {
def nextCount(): Int
}
/**
* This class is a concrete implementation of the [[Counter]] trait.
* It is configured for Guice dependency injection in the [[Module]]
* class.
*
* This class has a `Singleton` annotation because we need to make
* sure we only use one counter per application. Without this
* annotation we would get a new instance every time a [[Counter]] is
* injected.
*/
#Singleton
class AtomicCounter extends Counter {
private val atomicCounter = new AtomicInteger()
override def nextCount(): Int = atomicCounter.getAndIncrement()
}
You can also see its usage in this file:
my-test-app/app/controllers/CountController.scala
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
import services.Counter
/**
* This controller demonstrates how to use dependency injection to
* bind a component into a controller class. The class creates an
* `Action` that shows an incrementing count to users. The [[Counter]]
* object is injected by the Guice dependency injection system.
*/
#Singleton
class CountController #Inject() (counter: Counter) extends Controller {
/**
* Create an action that responds with the [[Counter]]'s current
* count. The result is plain text. This `Action` is mapped to
* `GET /count` requests by an entry in the `routes` config file.
*/
def count = Action { Ok(counter.nextCount().toString) }
}
This means every controller which has the constructor of #Inject() (counter: Counter) will receive the same instance of Counter.
So my question is:
Why use #Singleton and then #Inject it into a controller, when for this example you could just use a Scala object?
Its a lot less code.
Example:
my-test-app/app/services/Counter.scala
package services
trait ACounter {
def nextCount: Int
}
object Counter with ACounter {
private val atomicCounter = new AtomicInteger()
def nextCount(): Int = atomicCounter.getAndIncrement()
}
Use it like so:
my-test-app/app/controllers/CountController.scala
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
import services.{Counter, ACounter}
/**
* This controller demonstrates how to use dependency injection to
* bind a component into a controller class. The class creates an
* `Action` that shows an incrementing count to users. The [[Counter]]
* object is injected by the Guice dependency injection system.
*/
#Singleton
class CountController extends Controller {
//depend on abstractions
val counter: ACounter = Counter
def count = Action { Ok(counter.nextCount().toString) }
}
What is the difference? Is injection the preferred, and why?
Is injection the preferred way? Generally yes
A couple advantages of using dependency injection:
Decouple controller from the concrete implementation of Counter.
If you were to use an object, you would have to change your controller to point to the different implementation. EG Counter2.nextCount().toString
You can vary the implementation during testing using Guice custom bindings
Lets say that inside of Counter you are doing a WS call. This could cause some difficulty unit testing. If you are using dependency injection with Guice, you can override the binding between Counter and AtomicCounter to point to an offline version of Counter that you have written specifically for your tests. See here for more info on using Guice for Play tests.
Also see the motivations that Play had for migrating to DI.
I say generally because I've seen dependency injection go horribly wrong using Spring and other Java frameworks. I'd say you should use your own judgement but err on the side of using DI for Play.
Maybe because Scala's singleton object can't have parameters? For example, if you have a service class that has a DAO injected, and you want to use service in controller, you have to inject it. The easiest way(IMO) is DI with Guice... Also, you can have your dependencies in one place(module) etc...
I am not sure, if I understand your question, but injection is preferred because:
different parts of your application are less coupled
it is easier to replace your dependency with different class providing the same functionality (in case you would need to do that in future) - you will need to change few lines of code and not look for all occurrences of your object
it is simpler to test (especially when you need to mock something)
Shortly speaking: D from SOLID principles: "Depend upon Abstractions. Do not depend upon concretions".
I'm trying Play 2.4.2 for Scala and I'm not clear whether controllers should be defined as classes or singletons. The docs state:
A Controller is nothing more than a singleton object that generates
Action values.
However the code sample shows:
class Application extends Controller { ... }
To further complicate things, intellij gives me a warning if I define a class:
However I get a compilation error (but no warning) if I use a singleton:
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller { ... }
Error:(6, -1) Play 2 Compiler:
/Users/Toby/IdeaProjects/play-scala/conf/routes:6: type Application is
not a member of package controllers
Which approach is correct?
Your controllers should be objects if you use the static router. The static is the default router in Play 2.4 and has the same behavior from Play 2.3 and before.
You can convert your controllers to classes if you use the injected router which is new in Play 2.4. You need to enable the injected router in your build.sbt:
routesGenerator := InjectedRoutesGenerator
Update: the injected router is now the default in Play 2.5