Google Dagger Tutorial

  1. 1. Comparisons
  2. 2. Implementation
    1. 2.1 POJO
    2. 2.2 Module
    3. 2.3 Component
    4. 2.4 client side
  3. 3. Dagger User Guide
    1. 3.1 Declaring Dependencies
    2. 3.2 Satisfying Dependencies
    3. 3.3 Building the Graph
  4. Reference

1. Comparisons

Spring is a giant collection of libraries and utilities. with a lot of integration, an XML configuration, runtime/ reflective bindings. Application already using Spring can use its dependency injection smoothly.

Dependency is only a small part of it. Guice and Dagger is lightweight and only a dependency injection framework

Dagger is very lightweight framework with very few integrations, java interface/ annotation configuration, and compile-time code generated bindings.

For dependency injection and IOC container, there is a post in Chinese wrote last year.

Also a big difference between those DI framework is when does the injection happen, compile time or runtime?

Run-time DI is based on reflection which is simpler to use but slower at run-time —- Spring, Guice

Compile-time DI is based on code generation. This means that all the heavy-weight operations are performed during compilation. It adds complexity but generally performs faster

2. Implementation

  • POJO
  • Module
    • a class provides or builds the objects’ dependencies
  • Component
    • an interface used to generate the injector

2.1 POJO

public class Car {

    private Engine engine;
    private Brand brand;

    @Inject
    public Car(Engine engine, Brand brand) {
        this.engine = engine;
        this.brand = brand;
    }

    // getters and setters
}

2.2 Module

@Module // similar to @Controller  @Service 
public class VehiclesModule {
    @Provides // similar to @Bean 
    public Engine provideEngine() {
        return new Engine();
    }

    @Provides
    @Singleton
    public Brand provideBrand() { 
        return new Brand("lol"); 
    }
}

2.3 Component

here we could return the real object we want to be the starting point of the whole mechanism:

Dagger will start from here, go through all the @Inject and satisfy those dependencies. In our example, will create engine and brand object.

@Singleton
@Component(modules = VehiclesModule.class)
public interface VehiclesComponent {
    Car buildCar();
}

2.4 client side

Notice: DaggerVehiclesComponent is created by dagger automatically.

VehiclesComponent component = DaggerVehiclesComponent.create();
Car eg = component.buildCar();

3. Dagger User Guide

3.1 Declaring Dependencies

Dagger constructs instances of application classes and satisfies their dependencies. It uses @Inject annotation to identify which constructors and fields it is interested in.

3.2 Satisfying Dependencies

By default, Dagger satisfies each dependency by constructing an instance of the requested type. It call new SomeObject() and setting its injectable fields.

  • @Inject
    • interfaces cannot be constructed
    • third party classes cannot be annotated
    • configurable objects must be configured
  • Instead, use @provides
    • all @provides methods should be named with a provide prefix and module classes are named with a Module suffix

3.3 Building the Graph

The @Inject and @Provides annotated classes form a graph of objects, linked by their dependencies. Build the application by an interface with methods that have no arguments and return the desired type.

Reference

  1. https://stackoverflow.com/questions/39688830/why-use-develop-guice-when-you-have-spring-and-dagger
  2. https://www.baeldung.com/dagger-2
  3. https://rskupnik.github.io/dependency-injection-in-pet-project-dagger2
  4. https://dagger.dev/

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 stone2paul@gmail.com

文章标题:Google Dagger Tutorial

文章字数:463

本文作者:Leilei Chen

发布时间:2020-02-05, 01:12:52

最后更新:2020-02-05, 01:14:18

原始链接:https://www.llchen60.com/Google-Dagger-Tutorial/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏