Android 安卓自动注入匕首模块在每一个我需要的

Android 安卓自动注入匕首模块在每一个我需要的,android,dependency-injection,dagger-2,Android,Dependency Injection,Dagger 2,在项目上成功实现Dagger后,我必须为我想要使用的每个类指定Dagger并注入模块,例如RestClient的Reformation,我想知道有没有办法将组件自动定义到类中 例如,我的实现是: public class CoreApplication extends MultiDexApplication { private static ProjectApplicationComponent component; private RestClient restClient;

在项目上成功实现Dagger后,我必须为我想要使用的每个类指定Dagger并注入模块,例如
RestClient
Reformation
,我想知道有没有办法将
组件自动定义到类中

例如,我的实现是:

public class CoreApplication extends MultiDexApplication {
    private static ProjectApplicationComponent component;
    private RestClient restClient;
    private Picasso picasso;
    private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();

        ...
        component = DaggerProjectApplicationComponent.builder()
                .contextModule(new ContextModule(this))
                .networkServiceModule(new NetworkServiceModule(ClientSettings.SERVER_URL))
                .build();

        restClient= component.apiService();
        picasso = component.getPicasso();
        handler = component.getHandler();
    }

    public static ProjectApplicationComponent getComponent() {
        return component;
    }
}
以及我定义的
ApplicationComponent
,我要注入模块的witch类、活动或片段:

@ActivitiesScope
@Component(dependencies = ProjectApplicationComponent.class)
public interface ApplicationComponent {
    void inject(PersonsRemoteRepository personsRemoteRepository);
}
PersonRemoteRepository
类,我想将其注入
RestClient
以使用改型

public class PersonsRemoteRepository implements PersonsRepository {
    @Inject
    private RestClient restClient;

    private final ApplicationComponent component;

    public PersonsRemoteRepository() {
        component = DaggerApplicationComponent.builder()
                .projectApplicationComponent(CoreApplication.getComponent())
                .build();

        component.inject(this);
    }

    ...
}
我的
RestClient
类是:

public interface RestClient {
    @Headers("Content-Type: application/json")
    @POST("/api/v1/getPersons")
    Observable<List<Person>> getPersons();
}
例如,简化的
PersonRemoteRepository
类应为:

public class PersonsRemoteRepository implements PersonsRepository {
    @Inject
    private RestClient restClient;

    public PersonsRemoteRepository() {

    }

    ...
}
提前谢谢


更新帖子

在此我的活动中,
inject(this)
在此代码行中不可用:

CoreApplication.getComponent().inject(this);
我的活动:

public class LoginActivity extends AppCompatActivity{
    @Inject
    PersonsRemoteRepository personsRemoteRepository;

    @Inject
    RestClient restClient;

    private LoginActivityBinding mBinding;
    private LoginMethodsToPageViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CoreApplication.getComponent().inject(this);

        mBinding = DataBindingUtil.setContentView(this, R.layout.login_activity);

        personsRemoteRepository = new PersonsRemoteRepository(restClient);
        viewModel = new LoginMethodsToPageViewModel(personsRemoteRepository, this, mBinding);
        mBinding.setViewModel(viewModel);
    }

    ...
}
在这个屏幕截图中,正如您所看到的,我没有
inject()
方法

PersonRemoteRepository
更改后的类:

public class PersonsRemoteRepository implements PersonsRepository {
    private RestClient restClient;

    @Inject
    PersonsRemoteRepository(RestClient restClient) {
        this.restClient = restClient;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Observable<List<Person>> getAllPersons() {
        Observable<List<Person>> observable = restClient.getPersons();

        return observable
                .flatMap((Function<List<Person>, Observable<List<Person>>>) Observable::fromArray);
    }
}
公共类PersonsRemoteRepository实现PersonsRepository{
私人客户;
@注入
PersonRemoteRepository(RestClient RestClient){
this.restClient=restClient;
}
@抑制警告(“未选中”)
@凌驾
公众可观察的getAllPersons(){
Observable-Observable=restClient.getPersons();
可观测回波
.flatMap((函数)可观察::fromArray);
}
}

您不需要为每个要注入依赖项的类构建组件。依赖项可以通过用
@Inject
注释的构造函数提供:

公共类PersonsRemoteRepository实现PersonsRepository{
私人客户;
@注入
公共PersonRemoteRepository(RestClient RestClient){
this.restClient=restClient;
}
}
需要此存储库的任何其他类都可以执行相同的操作:

公共类任何其他类{
私有PersonRemoteRepository PersonRemoteRepository;
@注入
公共任意其他类(PersonRemoteRepository PersonRemoteRepository){
this.personsRemoteRepository=personsRemoteRepository;
}
对于Android创建的实例类,如应用程序、活动和片段,只需使用
component.inject

公共类MyActivity{
@注入PersonRemoteRepository PersonRemoteRepository;
@凌驾
public void onCreate(){
super.onCreate();
CoreApplication.getComponent().inject(此);
}
}
Core应用程序中需要的更改:

public class CoreApplication extends MultiDexApplication {
    private static ProjectApplicationComponent component;

    @Inject private RestClient restClient;
    @Inject private Picasso picasso;
    @Inject private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();

        ...
        component = DaggerProjectApplicationComponent.builder()
                .contextModule(new ContextModule(this))
                .networkServiceModule(new NetworkServiceModule(ClientSettings.SERVER_URL))
                .build();

        component.inject(this);
    }
}
应用程序组件中需要的更改:

@ActivitiesScope
@Component(dependencies = ProjectApplicationComponent.class)
public interface ApplicationComponent {
    void inject(CoreApplication coreApplication);

    void inject(MyActivity myActivity);
}

您不需要为每个要注入依赖项的类构建组件。依赖项可以通过用
@inject
注释的构造函数提供:

公共类PersonsRemoteRepository实现PersonsRepository{
私人客户;
@注入
公共PersonRemoteRepository(RestClient RestClient){
this.restClient=restClient;
}
}
需要此存储库的任何其他类都可以执行相同的操作:

公共类任何其他类{
私有PersonRemoteRepository PersonRemoteRepository;
@注入
公共任意其他类(PersonRemoteRepository PersonRemoteRepository){
this.personsRemoteRepository=personsRemoteRepository;
}
对于Android创建的实例类,如应用程序、活动和片段,只需使用
component.inject

公共类MyActivity{
@注入PersonRemoteRepository PersonRemoteRepository;
@凌驾
public void onCreate(){
super.onCreate();
CoreApplication.getComponent().inject(此);
}
}
Core应用程序中需要的更改:

public class CoreApplication extends MultiDexApplication {
    private static ProjectApplicationComponent component;

    @Inject private RestClient restClient;
    @Inject private Picasso picasso;
    @Inject private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();

        ...
        component = DaggerProjectApplicationComponent.builder()
                .contextModule(new ContextModule(this))
                .networkServiceModule(new NetworkServiceModule(ClientSettings.SERVER_URL))
                .build();

        component.inject(this);
    }
}
应用程序组件中需要的更改:

@ActivitiesScope
@Component(dependencies = ProjectApplicationComponent.class)
public interface ApplicationComponent {
    void inject(CoreApplication coreApplication);

    void inject(MyActivity myActivity);
}

有两个问题,如何注入到
CoreApplication
和如何注入到活动中。并且有两个相应的组件,
ProjectApplicationComponent
ApplicationComponent
,通过组件依赖性连接


要注入应用程序,Gustavo的回答很有用:

  • CoreApplication
    的字段注释为
    @Inject
  • ProjectApplicationComponent
    中的调配方法替换为成员注入方法:

    @ApplicationScope
    @组成部分(
    模块={
    ContextModule.class,
    NetworkServiceModule.class,
    ...,
    })
    公共接口项目应用程序组件{
    //成员注入法
    孔隙注入(CoreApplication CoreApplication);
    }
    
  • 构造一个
    ProjectApplicationComponent
    并调用
    inject
    方法:

    //CoreApplication.onCreate
    组成部分=
    DaggerProjectApplicationComponent.builder()
    .contextModule(新的contextModule(本))
    .networkServiceModule(…)
    .build();
    component.inject(/*coreApplication=*/this);
    

要将成员注入到
LoginActivity
,依赖的
ApplicationComponent
应具有成员注入方法:

@ActivitiesScope
@组件(依赖项=ProjectApplicationComponent.class)
公共接口应用程序组件{
无效注入(LoginActivity LoginActivity);
}
回想一下,您的
LoginActivity
有两个
@Inject
ed字段,类型分别为
RestClient
PersonRemoteRepository

公共类LoginActivity扩展了AppCompatActivity{
@注入PersonRemoteRepository PersonRemoteRepository;
@注入RestClient-RestClient;
}
为了使依赖的
ApplicationComponent
获得
RestClient
,t