Skip to content

Instantly share code, notes, and snippets.

@urcadox
Last active January 22, 2020 11:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urcadox/dbddece71053b46754a2ee83d6ae3993 to your computer and use it in GitHub Desktop.
Save urcadox/dbddece71053b46754a2ee83d6ae3993 to your computer and use it in GitHub Desktop.
Creating an instance of a class with injected components in Play 2.5.x / 2.6.x / 2.7.x / 2.8.x REPL
// For Play 2.5.x and 2.6.x
// After starting the application in the console (https://www.playframework.com/documentation/2.5.x/PlayConsole#launch-the-interactive-console)
import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.createContext(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
// Do this:
app.injector.instanceOf[full.path.YourClass](app.classloader.loadClass("full.path.YourClass").asInstanceOf[Class[full.path.YourClass]])
// For Play 2.7.x and 2.8.x
// After starting the application in the console https://www.playframework.com/documentation/2.8.x/PlayConsole#Launch-the-Scala-console)
import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.Context.create(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
// Do this:
app.injector.instanceOf[full.path.YourClass](app.classloader.loadClass("full.path.YourClass").asInstanceOf[Class[full.path.YourClass]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment