Creating an injectable util-app App

To create an injectable util-app App, first depend on the inject-app library. Then use the inject framework to create an injectable App. Finatra provides an injectable version of the util-app App trait: c.t.inject.app.App.

Extending the c.t.inject.app.App trait creates an injectable util-app App.

This allows for the use of dependency injection in a util-app App with support for modules which allows for powerful feature testing of the application.

Scala Example

import com.google.inject.Module
      import com.twitter.inject.app.App
      import com.twitter.inject.Logging

      object MyAppMain extends MyApp

      class MyApp extends App with Logging  {

        override val modules: Seq[Module] = Seq(
          MyModule1)

        override protected def run(): Unit = {
          // Core app logic goes here.
        }
      }

Then to test:

import com.twitter.inject.Test

class MyAppTest extends Test {

  private val app = EmbeddedApp(new MyApp)

  test("MyApp#runs") {
    app.main("some.flag1" -> "value1", "some.flag2" -> "value2")
  }
}

Java Example

import java.util.Collection;

import com.google.common.collect.ImmutableList;
import com.google.inject.Module;

import com.twitter.inject.app.AbstractApp;

public class MyApp extends AbstractApp {

    @Override
    public Collection<Module> javaModules() {
        return ImmutableList.<Module>of(
                MyModule1$.MODULE$);
    }

    @Override
    public void run() {
        // Core app logic goes here.
    }
}

See the Finatra examples for detailed examples with tests.

App#run

The single point of entry to the c.t.inject.app.App is the #run method.

The Finatra Startup Lifecycle ensures that the injector will be properly configured before access and provides the #run method as the function for implementing the app.