一项活动中来自两个组件的依赖性 [英] Dependencies from two components in one activity

查看:60
本文介绍了一项活动中来自两个组件的依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Dagger-2一起玩耍,以弄清楚如何将其集成到我们现有的应用程序中,并且遇到了一些我无法理解或做错的事情。

I'm playing with Dagger-2 to figure how to integrate it in our existing application and I'm facing something which I can't understand or I'm doing wrong.

我的情况:

3个没有任何带注释的构造函数的API(每个都在自己的文件中)

3 API without any annotated constructor ( each one in its own file )

public class DbApi {
   public void doSomething(String username, String password) {
   }
}

public class RestApi {
    public void doSomething(String username, String password) {
    }
}

public class SocketApi {
    public void doSomething(String username, String password) {
    }
}

3个模块(每个模块在其自己的文件)

3 Modules ( each one in its own file )

@Module
public class DbModule {
    @Provides
    @Singleton
    public DbApi getDbApi(){
        return new DbApi();
    }
}

@Module
public class RestModule {
    @Provides
    @Singleton
    public RestApi getRestApi(){
        return new RestApi();
    }
}

@Module
public class SocketModule {
    @Provides
    @Singleton
    public SocketApi getSocketApi(){
        return new SocketApi();
    }
}

2个组件(每个组件都在自己的文件中)

2 Components ( each one in its own file )

@Singleton
@Component(modules = {DbModule.class})
public interface DbComponent {
   void inject(SecondActivity activity);
}

@Singleton
@Component(modules = {RestModule.class, SocketModule.class})
public interface NetComponent {
   void inject(MainActivity activity);
}

我将这两个成分初始化为Aplication

I initialize the two components into my Aplication

public class MyApplication extends Application {

DbComponent dBComponent;
NetComponent netComponent;

@Override
public void onCreate() {
    super.onCreate();
    dBComponent = DaggerDbComponent.builder().build();
    netComponent = DaggerNetComponent.builder().build();
}

public NetComponent getNetComponent() {
    return netComponent;
}

public DbComponent getdBComponent() {
    return dBComponent;
}
}

在定义了所有这些之后,我将NetComponent用作一项活动

With all this defined I use the NetComponent into one activity and the DbCompoment into a second one.

进入第一个活动

public class MainActivity extends AppCompatActivity {

@Inject RestApi restApi;
@Inject SocketApi socketApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    ((MyApplication) getApplication()).getNetComponent().inject(this);
    //...
}
}

并进入第二个

public class SecondActivity extends AppCompatActivity {

@Inject DbApi dbApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    ((MyApplication) getApplication()).getdBComponent().inject(this);
    //...
}
}

最多在这里一切正常,我在每次活动中都注入了自己的东西。
但后来我意识到,在SecondActity中还需要 RestApi SocketApi ,所以我尝试注入

Up to here everything works fine and I got my stuff injected properlty in each activity. But then I have realised that RestApi and SocketApi were also required into the SecondActity so I tried to inject them into the activity and here I have doubts and issues.

我修改 NetComponent 以添加以下行

void inject(SecondActivity activity);

然后修改我的 SecondActivity

public class SecondActivity extends AppCompatActivity {

@Inject DbApi dbApi;
@Inject RestApi restApi;
@Inject SocketApi socketApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    ((MyApplication) getApplication()).getdBComponent().inject(this);
    ((MyApplication) getApplication()).getNetComponent().inject(this);
    //...
}
}

之后修改我的项目时出现以下错误

After those modifications compiling my project I have the following error

错误:(15,10)错误:com.example.guillaume.testdagger.RestApi如果没有@Inject构造函数就无法提供或通过@Provides或@Produces注释的方法。
com.example.guillaume.testdagger.SecondActivity.restApi
[注入的字段类型:com.example.guillaume.testdagger.RestApi restApi]

Error:(15, 10) error: com.example.guillaume.testdagger.RestApi cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. com.example.guillaume.testdagger.SecondActivity.restApi [injected field of type: com.example.guillaume.testdagger.RestApi restApi]

错误:(16,10)错误:没有@Inject构造函数或@ Provides-或@ Produces-注释方法无法提供com.example.guillaume.testdagger.DbApi。
com.example.guillaume.testdagger.SecondActivity.dbApi
[注入的字段类型:com.example.guillaume.testdagger.DbApi dbApi]

Error:(16, 10) error: com.example.guillaume.testdagger.DbApi cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. com.example.guillaume.testdagger.SecondActivity.dbApi [injected field of type: com.example.guillaume.testdagger.DbApi dbApi]

所以我的问题是:


  • 此错误来自何处?

  • 如何使用几个组件将字段注入一个活动中

  • 我做错了吗?将组件/模块组织起来以便能够将一个组件复用到已经包含另一个组件的活动中的最佳方法是什么? module

  • 我可以通过在api类中添加并添加带@Inject注释的空公共构造函数来解决该错误,但我对此感到失望,因为我将所有字段都注入了 SecondActivity 不管用于调用 inject 方法的组件是什么,都是正确的方法。

  • Where this error comes from?
  • How can I use several components to inject fields into one activity
  • Am I doing this wrong and what will be the best way to organize components/modules in order to be able to reuse one compoment into an activity already containing another module
  • I can fix the error by adding and empty public constructor annotated with @Inject to my api classes but doing this I discoreved that all my fields will be injected into the SecondActivity regardless of the component used to invoke the inject method, is it the right way to do this.

推荐答案

啊...我很痛苦。
您不能将两个组件注入到同一活动中。至少我找不到方法! :)

Ah...I share your pain. You can't inject two components into same Activity. At least I was not able to find how! :)

这对我来说也是很合理的尝试,而且我也遇到了同样的问题。

That was logical for me to try, as well, and I faced same problems.

您可以通过两种方法解决它:

You can resolve it with 2 approach:


  1. 使用一个大组件所有注射(我真的是这样)

  2. 为每个要注射某种东西的活动/场所创建一个组件

因此,而不是

@Singleton
@Component(modules = {RestModule.class, SocketModule.class})
public interface NetComponent {
   void inject(MainActivity activity);
}

尝试

@Singleton
@Component(modules = {RestModule.class, SocketModule.class, DbModule.class})
public interface SecondActivityComponent {
   void inject(SecondActivity activity);
}

我发现第二个是最干净的解决方案。

I found second one as cleanest solution.

如果有人有其他建议,我准备尝试!

If anybody has some other advice, I am ready to try it!

这篇关于一项活动中来自两个组件的依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆