Giter Club home page Giter Club logo

play-java-dagger2-example's Introduction

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;
    }    
}

play-java-dagger2-example's People

Contributors

wsargent avatar gmethvin avatar marcospereira avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.