dagger

@Component.Builder

自定义一个Builder接口,在接口中可以自定义一些接口
接口包括:

  • 当前Component的返回值
  • Module中需要用到的类
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
@ActivityScoped
@Component(modules = UserModule.class, dependencies = AppComponent.class)
public interface UserComponent {
void inject(MActivity activity);
}



public static final class Builder {
private UserModule userModule;

private AppComponent appComponent;

private Builder() {}

public UserComponent build() {
if (userModule == null) {
throw new IllegalStateException(UserModule.class.getCanonicalName() + " must be set");
}
if (appComponent == null) {
throw new IllegalStateException(AppComponent.class.getCanonicalName() + " must be set");
}
return new DaggerUserComponent(this);
}

public Builder userModule(UserModule userModule) {
this.userModule = Preconditions.checkNotNull(userModule);
return this;
}

public Builder appComponent(AppComponent appComponent) {
this.appComponent = Preconditions.checkNotNull(appComponent);
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
@ActivityScoped
@Component(modules = UserModule.class, dependencies = AppComponent.class)
public interface UserComponent {

void inject(MActivity activity);

@Component.Builder
interface Builder{
UserComponent build();
Builder buildAppConponent(AppComponent appComponent);
Builder userModule(UserModule userModule);
}
}



private static final class Builder implements UserComponent.Builder {
private UserModule userModule;

private AppComponent appComponent;

@Override
public UserComponent build() {
if (userModule == null) {
throw new IllegalStateException(UserModule.class.getCanonicalName() + " must be set");
}
if (appComponent == null) {
throw new IllegalStateException(AppComponent.class.getCanonicalName() + " must be set");
}
return new DaggerUserComponent(this);
}

@Override
public Builder buildAppConponent(AppComponent appComponent) {
this.appComponent = Preconditions.checkNotNull(appComponent);
return this;
}

@Override
public Builder userModule(UserModule userModule) {
this.userModule = Preconditions.checkNotNull(userModule);
return this;
}
}

@BindsInstance

当你的Module中的Provides需要参数时,可以不在Module中处理,而是在自定义Builder中声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Module
public class UserModule {

@Provides
public SharedPreferences providePreferences(Application application) {
return application.getSharedPreferences("1",
Context.MODE_PRIVATE);
}
}


@Component(modules = UserModule.class)
public interface UserComponent {
void inject(MActivity activity);

SharedPreferences getSharedPrefs();

@Component.Builder
interface Builder {
UserComponent build();
@BindsInstance Builder application(Application application);
}
}

可以看到在Application的接口前面加了一个注解,dagger在创建Component实现类的时候,同时会帮你创建application的实现类

1
2
3
4
5
@Override
public Builder application(Application application) {
this.application = Preconditions.checkNotNull(application);
return this;
}

如果按照正常方式写,你需要这样写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Module
public class UserModule {

Application application;

UserModule(Application application){
this.application = application;
}

@Provides
public SharedPreferences providePreferences() {
return application.getSharedPreferences("1",
Context.MODE_PRIVATE);
}

}

SubCompoment

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
@Subcomponent(modules = PersonModule.class)
public interface PersonComponent {
void inject(MActivity m);
}


@Module
public class PersonModule {

@Provides
public UserData providersUserData(){
UserData userData = new UserData(null);
userData.setName("thisisname");
return userData;
}
}

@Component(modules = UserModule.class)
public interface UserComponent {

PersonComponent add(PersonModule personModule);

SharedPreferences getSharedPrefs();

@Component.Builder
interface Builder {
UserComponent build();
@BindsInstance Builder application(Application application);

}
}


DaggerUserComponent.builder().application(getApplication()).build().add(new PersonModule()).inject(this);

这样,PersonModule中所provide的类,可以直接注入到当前的Activity中

主Component可以使用SubComponent的方法和提供的类,通过构建过程中传入SubComponent所需要的对象

ContributesAndroidInjector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Singleton
@Component(modules = {TasksRepositoryModule.class,
ApplicationModule.class,
ActivityBindingModule.class,
AndroidSupportInjectionModule.class})
public interface AppComponent extends AndroidInjector<ToDoApplication> {
@Component.Builder
interface Builder {

@BindsInstance
AppComponent.Builder application(Application application);

AppComponent build();
}
}

ActivityBindingModule:

1
2
3
4
5
6
@Module
public abstract class ActivityBindingModule {
@ActivityScoped
@ContributesAndroidInjector(modules = TasksModule.class)
abstract TasksActivity tasksActivity();
}

在ActivityBindingModule类中,使用了ContributesAndroidInjector注解,他的用处就是根据你提供的类来新建Component类,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Module(subcomponents = ActivityBindingModule_TasksActivity.TasksActivitySubcomponent.class)
public abstract class ActivityBindingModule_TasksActivity {
private ActivityBindingModule_TasksActivity() {}

@Binds
@IntoMap
@ActivityKey(TasksActivity.class)
abstract AndroidInjector.Factory<? extends Activity> bindAndroidInjectorFactory(
TasksActivitySubcomponent.Builder builder);

@Subcomponent(modules = TasksModule.class)
@ActivityScoped
public interface TasksActivitySubcomponent extends AndroidInjector<TasksActivity> {
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<TasksActivity> {}
}
}

就是我们常见的自己手动写的Module和Component,并且我们使用的Scope他同样也会带上
如果你需要注入的类包含Fragment,他会专门生成一个SubComponent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Module(subcomponents = TasksModule_TasksFragment.TasksFragmentSubcomponent.class)
public abstract class TasksModule_TasksFragment {
private TasksModule_TasksFragment() {}

@Binds
@IntoMap
@FragmentKey(TasksFragment.class)
abstract AndroidInjector.Factory<? extends Fragment> bindAndroidInjectorFactory(
TasksFragmentSubcomponent.Builder builder);

@Subcomponent
@FragmentScoped
public interface TasksFragmentSubcomponent extends AndroidInjector<TasksFragment> {
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<TasksFragment> {}
}
}

在生成的主Componet中会对这些SubComponent进行接口的实现:

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
private final class TasksActivitySubcomponentImpl
implements ActivityBindingModule_TasksActivity.TasksActivitySubcomponent {
private Provider<TasksModule_TasksFragment.TasksFragmentSubcomponent.Builder>
tasksFragmentSubcomponentBuilderProvider;

@SuppressWarnings("rawtypes")
private Provider tasksPresenterProvider;

private Provider<
Map<
Class<? extends android.support.v4.app.Fragment>,
Provider<AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>>>
mapOfClassOfAndProviderOfFactoryOfProvider;

private DispatchingAndroidInjector_Factory dispatchingAndroidInjectorProvider;

private Provider<TasksFragment> tasksFragmentProvider;

private TasksActivitySubcomponentImpl(TasksActivitySubcomponentBuilder builder) {
initialize(builder);
}

...


@Override
public void inject(TasksActivity arg0) {
injectTasksActivity(arg0);
}

private TasksActivity injectTasksActivity(TasksActivity instance) {
DaggerAppCompatActivity_MembersInjector.injectSupportFragmentInjector(
instance, getDispatchingAndroidInjectorOfFragment());
...
return instance;
}

private final class TasksFragmentSubcomponentBuilder
extends TasksModule_TasksFragment.TasksFragmentSubcomponent.Builder {
private TasksFragment seedInstance;

...
}

private final class TasksFragmentSubcomponentImpl
implements TasksModule_TasksFragment.TasksFragmentSubcomponent {
private TasksFragmentSubcomponentImpl(TasksFragmentSubcomponentBuilder builder) {}

@Override
public void inject(TasksFragment arg0) {
injectTasksFragment(arg0);
}

private TasksFragment injectTasksFragment(TasksFragment instance) {
DaggerFragment_MembersInjector.injectChildFragmentInjector(
instance, TasksActivitySubcomponentImpl.this.getDispatchingAndroidInjectorOfFragment());
TasksFragment_MembersInjector.injectMPresenter(
instance,
(TasksContract.Presenter)
TasksActivitySubcomponentImpl.this.tasksPresenterProvider.get());
return instance;
}
}
}

创建基本类后,生成对应的Factory类,提供每个类的对象,这些类都实现了Provider接口

1
2
3
4
5
6
7
8

public interface Factory<T> extends Provider<T> {
}


public interface Provider<T> {
T get();
}

Factory类的主要工作是根据传进来的Provider创建新的实例

1
2
3
4
5
6
7
8
9
10
private Provider tasksPresenterProvider;

this.tasksPresenterProvider =
DoubleCheck.provider(
TasksPresenter_Factory.create(DaggerAppComponent.this.tasksRepositoryProvider));


public static TasksPresenter_Factory create(Provider<TasksRepository> tasksRepositoryProvider) {
return new TasksPresenter_Factory(tasksRepositoryProvider);
}

  • 声明Provider接口对象
  • 创建所有对应的Provider的Factory
  • 根据所需参数传入,创建对应的Factory(也就是创建一个,看构造函数需要什么,如果没有,就创建对应Factoy)
  • 进行注入
1
2
3
4
5
6
7
8
9
public static void injectInjectedFragment(
TaskDetailActivity instance, TaskDetailFragment injectedFragment) {
instance.injectedFragment = injectedFragment;
}

public static void injectMPresenter(
TaskDetailFragment instance, TaskDetailContract.Presenter mPresenter) {
instance.mPresenter = mPresenter;
}