[JAVA] Super rough! How to install Dagger2

About DI

I think there are various articles, so I will omit it! Roughly speaking, a method of injecting instances from outside the class to reduce coupling! If you prepare an interface and inject it into it, it will be easier to test.

Masakari is welcome! !! !! Please let me know if something is wrong or missing ...!

Where it gets stuck

I would like to introduce the points that I got stuck when introducing it on Android first.

What's stuck

There was not enough way to specify the dependency in Gradle & I was specifying the dependency that is not necessary at this stage.

How did you make a mistake

Dagger2 Official #How do I get it? I wrote only the dependencies written here, not the Java dependencies.

Introduction method

It was faster to look at the Github ReadMe than the official one. ..

Add the following code to gradle under app and sync.

dependencies {
    compile 'com.google.dagger:dagger:2.14.1'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

Now you can inject the instance externally. This is enough when you want to separate external knowledge (WebAPI, etc.) and infrastructure layer (repository). I haven't investigated when dagger.android can be used, so please let me know if you like m (_ _) m

Roughly how to inject

Just create a Module, define what you want to inject into the Component, and specify @Inject. It supports field and constructor injection.

You can think of Module as a so-called DI container (should). The idea is to cut out the instance generation separately and manage it in one place.

First, create a Module.

//It is a good idea to add Module to suffix.
public class InfrastructureModule {
    //It is a good idea to add provide to the prefix.
    public RepositoryInterafce provideXYZRepository() {
        return new XYZRepository();

I write it like this. If you specify @ Singleton, Dagger will handle the generation as a singleton.

Next, create a Component.

Component is an interface that defines where to inject any Module you create. Write like this.

//It is a good idea to add Component to suffix.
//Because it handles singleton [email protected] Singleton. Otherwise, the build will throw an error.
@Component(Modules = {InfrastructureModule.class})
public class InfrastructureComponent {
    //The method name is inject. It's full of manners, but if you get used to it, you can play brain muscles! w
    void inject(FugaHoge fugaHoge);
    void inject(FooBar fooBar);

After that, add @Inject and inject it as a field or a constructor.

public class FugaHoge {
   //Try field injection
   @Inject RepositoryInterface repository;

   public FugaHoge() {
        //If you run Rebuild, prefix:The component entity of Dagger is automatically generated.
        //Inject in the constructor.

This will inject the instance into repository.


Recommended Posts

Super rough! How to install Dagger2
How to install Docker
How to install docker-machine
How to install MySQL
How to install ngrok
[Rails] How to install devise
How to install Boots Faces
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
How to install JDK8-10 (Mac)
[Rails] How to install Font Awesome
How to install ImageMagick on Windows 10
[How to install Spring Data Jpa]
How to install Bootstrap in Ruby
How to install MariaDB 10.4 on CentOS 8
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
How to install WildFly on Ubuntu 18.04
Super easy in 2 steps! How to install devise! !! (rails 5 version)
How to install Swiper in Rails
How to blur an image (super easy)
How to install Eclipse (Photon) on Mac
How to install production Metabase on Ubuntu
How to install beta php8.0 on CentOS8
How to install the legacy version [Java]
How to install kafkacat on Amazon Linux2
How to deploy
How to use Swift's Codable Super personal memo
How to install network drivers on standalone Ubuntu
How to install Titan2D (v4.2.0) in virtual environment
How to install NVIDIA driver on Ubuntu 18.04 (Note)
How to install multiple JDKs on Ubuntu 18.04 LTS
How to Install Oracle JDK 1.8 in Ubuntu 18.04 LTS?
[Ruby on Rails] How to install Bootstrap in Rails
[For super beginners] How to use autofocus: true
How to install Ruby on an EC2 instance on AWS
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to write Rails
How to use rbenv
How to install & import Auto Gluon with Google Colaboratory
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to install the root certificate of Centos7 (Cybertrust)
How to install NVIDIA driver on Ubuntu ssh destination
How to adapt Bootstrap
How to install geckodriver (Selenium WebDriver) automatically using WebDriverManager
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to write dockerfile
How to uninstall Rails
[How to use label]
How to make shaded-jar
How to write docker-compose
How to use identity
How to use hashes