Dependency Injection

Finatra internally uses the Google Guice dependency injection library extensively which is also available for service writers if they choose to use dependency injection.

Attention

You are not required to use Guice dependency injection when using Finatra. Creating servers, wiring in controllers and applying filters can all be done without using any dependency injection. However, you will not be able to take full-advantage of Finatra’s testing features.

An example of Finatra’s dependency-injection integration is adding controllers to Finatra’s HttpRouter by type:

class Server extends HttpServer {
  override def configureHttp(router: HttpRouter) {
    router.add[MyController]
  }
}

As mentioned, it is also possible to do this without using Guice, simply instantiate your controller and add the instance to the router:

class NonDIServer extends HttpServer {
  val myController = new MyController(...)

  override def configureHttp(router: HttpRouter) {
    router.add(myController)
  }
}