Introduction to the use of Dagger2 dependency injection framework

How to introduce Dagger2

  1. Configure the apt plugin (add the following code in build.gradle(Project:xxx))

    dependencies {
          
          
        classpath 'com.android.tools.build:gradle:2.1.0'
        //添加apt插件
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  
    }
    
  2. Add dependency (add the following code in build.gradle(Module:app))

    apply plugin: 'com.android.application'
    //添加如下代码,应用apt插件`
    apply plugin: 'com.neenbedankt.android-apt'
    ...
    dependencies {
          
          
       ...
       compile 'com.google.dagger:dagger:2.4'
       apt 'com.google.dagger:dagger-compiler:2.4'
       //java注解
       compile 'org.glassfish:javax.annotation:10.0-b28'
       ...
    }
    
  3. Don't forget to add lint warning

    android {
          
          
    		...
                
    lintOptions {
          
          
    	warning 'InvalidPackage'
    	}
    }
    

.

Use of Dagger2

Classes that need to be injected with dependencies

public class MainPresenter {
    
    
   //MainContract是个接口,View是他的内部接口,这里看做View接口即可
   private MainContract.View mView;
   
   @Inject
   MainPresenter(MainContract.View view) {
    
    
       mView = view;
   }    
   public void loadData() {
    
    
       //调用model层方法,加载数据
       ...
       //回调方法成功时
       mView.updateUI();
   }

Function:
@Inject The attribute or construction method with this annotation will participate in dependency injection, and Dagger2 will instantiate the class with this annotation

.

Module class

@Module
public class MainModule {
    
    
    private final MainContract.View mView;

    public MainModule(MainContract.View view) {
    
    
        mView = view;
    }

    @Provides
    MainView provideMainView() {
    
    
        return mView;
    }
}

Function:
@Module class with this annotation is used to provide dependencies. It defines some methods annotated with @Provides beginning with provide. These methods are the provided dependencies. Dagger2 will look for and instantiate a certain class in this class The dependencies needed.

.

Component interface

@Component(modules = MainModule.class)
public interface MainComponent {
    
    
    void inject(MainActivity activity);
}

Function:
@Component is used to connect @Inject and @Module, obtain dependencies from @Module and inject them to @Inject

Use in Activity

public class MainActivity extends AppCompatActivity implements MainContract.View {
    
    

    @Inject
    MainPresenter mainPresenter;
    ...
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
         DaggerMainComponent.builder()
                .mainModule(new MainModule(this))//绑定Module,并传递Activity的view对象
                .build()
                .inject(this);
        //调用Presenter方法加载数据
         mainPresenter.loadData();
         
         ...
    }

}

Let’s first look at the code in MainActivity. Before we declared MainPresenter directly, we now add an annotation @Inject to the declaration, indicating that MainPresenter needs to be injected into MainActivity, that is, MainActivity depends on MainPresenter. Here, we should pay attention to the use of @ When Inject, you cannot use the private modifier to modify the member attributes of the class.

Then we also added the @Inject annotation to the constructor of MainPresenter. In this way, the mainPresenter in MainActivity establishes a certain connection with his constructor. We can understand this connection in this way. When a certain class is marked by @Inject, it will go to its construction method. If the construction method is also marked by @Inject, this class will be automatically initialized to complete the dependency. injection.

Then, they will not establish a connection out of thin air, so we thought that a bridge must be needed to connect them, which is the Component to be introduced below.

Component is an interface or abstract class, annotated with @Component annotation (here, regardless of the modules in brackets), we define an inject() method in this interface, and the parameter is Mainactivity. Then rebuild the project, a Component class prefixed with Dagger will be generated, here is DaggerMainComponent, and then complete the following code in MainActivity.

DaggerMainComponent.builder()
                .mainModule(new MainModule(this))
                .build()
                .inject(this);

.

Reference materials:

Guess you like

Origin blog.csdn.net/weixin_42324979/article/details/109571417