hayd / play-java-dagger2-example

Play Application using Dagger 2 for Compile Time DI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

play-java-dagger2-example

This project shows how to use Play Java with Dagger 2.

Running

sbt run

Then go to http://localhost:9000 to see the time and change the time zone.

Go to http://localhost:9000/ws to see the WS client pull the time from a remote service (really the app itself).

Background

Dagger 2 is a compile time dependency injection system. This means that dependencies are still declared with @Inject, but the compiler is responsible for resolving the graph.

Play Java supports Compile Time Dependency Injection so the work here is to provide an application loader that hooks into Dagger, rather than using constructor based DI.

The dagger.MyApplicationLoader class provides the core, by calling out to the DaggerApplicationComponent:

public class dagger.MyApplicationLoader implements ApplicationLoader {

    @Override
    public Application load(Context context)
    {
        final ClassLoader classLoader = context.environment().classLoader();
        final Optional<LoggerConfigurator> opt = LoggerConfigurator.apply(classLoader);
        opt.ifPresent(lc -> lc.configure(context.environment(), context.initialConfig(), emptyMap()));

        ApplicationComponent applicationComponent = DaggerApplicationComponent.builder()
                .applicationLoaderContextModule(new ApplicationLoaderContextModule(context))
                .build();

        return applicationComponent.application();
    }
}

From there, it's a question of providing components by extending BuiltInComponentsFromContext. The ClockModule is included to show that you can provide your own custom modules to Dagger.

public class MyComponentsFromContext extends BuiltInComponentsFromContext
        implements NoHttpFiltersComponents, AssetsComponents, AhcWSComponents, FormFactoryComponents, BodyParserComponents {

    private final Clock clock;

    @Inject
    public MyComponentsFromContext(ApplicationLoader.Context context, Clock clock) {
        super(context);
        this.clock = clock;
    }

    private TimeController timeController() {
        return new controllers.TimeController(clock, wsClient(), formFactory());
    }

    @Override
    public play.routing.Router router() {
        Router routes = new Routes(scalaHttpErrorHandler(), timeController(), assets());
        return routes.asJava();
    }
}

SimpleInjector

There is a small amount of extra configuration, because the Java annotation system still requires a small amount of runtime dependency injection -- this is fixed by putting a couple of extra mappings into a delegating injector.

public class MyComponentsFromContext {
    @Override
    public Injector injector() {
        // This probably should be solved by BuiltInComponentsFromContext itself
        Injector injector = super.injector();
    
        Map<Class, Supplier<Object>> extraMappings = new HashMap<>();
        SimpleInjector simpleInjector = new SimpleInjector(injector, extraMappings);
    
        extraMappings.put(JavaHandlerComponents.class, () -> new DefaultJavaHandlerComponents(simpleInjector.asScala(), actionCreator(), httpConfiguration(), executionContext(), javaContextComponents()));
        extraMappings.put(play.mvc.BodyParser.Default.class, this::defaultParser);
    
        return simpleInjector;
    }    
}

About

Play Application using Dagger 2 for Compile Time DI

License:Other


Languages

Language:Java 81.8%Language:HTML 8.5%Language:Shell 5.7%Language:Scala 3.5%Language:JavaScript 0.4%