当前位置:   article > 正文

android 注入框架-Dagger2-Module构造方法传参_dagger2传参

dagger2传参

1.环境创建

Module:app
dependencies {
    implementation 'com.google.dagger:dagger:2.8'     
    annotationProcessor "com.google.dagger:dagger-compiler:2.8" 
    annotationProcessor 'org.glassfish:javax.annotation:10.0-b28'  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.创建实例化的类

package com.example.dagger2test2;

import android.util.Log;

public class LoginCtrl {
    private LoginStore mLoginStore;
    public LoginCtrl(LoginStore store){
        mLoginStore = store;
    }
    public void login(String name,String pass){
        Log.e("tag@@", "name:"+name+" pass:"+pass);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.创建module

package com.example.dagger2test2;

import android.content.Context;

import dagger.Module;
import dagger.Provides;

@Module
public class LoginModule {
    private Context mContext;   //供给LoginStore使用
    public LoginModule(Context context){
        mContext = context;
    }

    //为LoginStore提供构造参数
    @Provides
    Context provideStoreContext(){
        return mContext;
    }

    @Provides
    LoginCtrl provideLoginCtrl(LoginStore storet){
        return new LoginCtrl(storet);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

4.创建Component接口

package com.example.dagger2test2;

import dagger.Component;

@Component(modules = LoginModule.class)
public interface LoginComponent {
    //要添加方法,方法必须添加参数,参数类型必须和调用时候一致
    void inject(MainActivity activity);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5.创建实例化的类的参数LoginStore

package com.example.dagger2test2;

import android.content.Context;

import javax.inject.Inject;

public class LoginStore {
    private Context mContext;

    //这里@Inject构造方法,然后在Module类里面还需要Provide一个String的方法
    @Inject
    public LoginStore(Context mContext) {
        this.mContext = mContext;
        MainActivity activity = (MainActivity)mContext;
        activity.changeHelloWord("LoginStore call");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

6.在Activity注入

package com.example.dagger2test2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import javax.inject.Inject;

public class MainActivity extends AppCompatActivity {

    @Inject
    LoginCtrl loginCtrl;    //注入的方式实例化LoginCtrl

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化注入,inject是LoginComponent接口里面自定义的方法;
        DaggerLoginComponent.builder()
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);

        // 然后就可以调用loginCtrl里面的方法了
        loginCtrl.login("tianping","pass");
    }

    public void changeHelloWord(String str){
        TextView textView = findViewById(R.id.hl_id);
        textView.setText(str);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

运行效果
运行效果
在这里插入图片描述
7.关系分析
dagger2关系分析

8.生成的源码
dagger2生成的源码

// Generated by dagger.internal.codegen.ComponentProcessor (https://google.github.io/dagger).
package com.example.dagger2test2;

import android.content.Context;
import dagger.MembersInjector;
import dagger.internal.Preconditions;
import javax.inject.Provider;

public final class DaggerLoginComponent implements LoginComponent {
  private Provider<Context> provideStoreContextProvider;

  private Provider<LoginStore> loginStoreProvider;

  private Provider<LoginCtrl> provideLoginCtrlProvider;

  private MembersInjector<MainActivity> mainActivityMembersInjector;

  private DaggerLoginComponent(Builder builder) {
    assert builder != null;
    initialize(builder);
  }

  public static Builder builder() {
    return new Builder();
  }

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {

    this.provideStoreContextProvider =
        LoginModule_ProvideStoreContextFactory.create(builder.loginModule);

    this.loginStoreProvider = LoginStore_Factory.create(provideStoreContextProvider);

    this.provideLoginCtrlProvider =
        LoginModule_ProvideLoginCtrlFactory.create(builder.loginModule, loginStoreProvider);

    this.mainActivityMembersInjector =
        MainActivity_MembersInjector.create(provideLoginCtrlProvider);
  }

  @Override
  public void inject(MainActivity activity) {
    mainActivityMembersInjector.injectMembers(activity);
  }

  public static final class Builder {
    private LoginModule loginModule;

    private Builder() {}

    public LoginComponent build() {
      if (loginModule == null) {
        throw new IllegalStateException(LoginModule.class.getCanonicalName() + " must be set");
      }
      return new DaggerLoginComponent(this);
    }

    public Builder loginModule(LoginModule loginModule) {
      this.loginModule = Preconditions.checkNotNull(loginModule);
      return this;
    }
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/614227
推荐阅读
相关标签
  

闽ICP备14008679号