Dagger 组件依赖含义
Dagger component dependency meaning
我正在试验 Dagger 2,我只是在测试一些东西以了解框架。
我有一个 ApplicationComponent 需要是整个应用程序的单例,所以我这样定义它:
@Component(modules = {ApplicationModule.class})
@Singleton
public interface ApplicationComponent {
Context provideContext();
}
含模块:
@Module
public class ApplicationModule {
private Application appContext;
public ApplicationModule(Application appContext) {
this.appContext = appContext;
}
@Provides
@Singleton
public Context provideContext() {
return appContext;
}
}
现在我还想要一个 NetworkComponent,它需要在应用程序运行期间一直运行。
该网络组件需要依赖于 ApplicationComponent。
所以我的网络组件如下:
@Component(dependencies = {ApplicationComponent.class}, modules = {NetworkModule.class})
@PerApp
public interface NetworkComponent extends ApplicationComponent {
@Named(DaggerConstants.DEFAULT_RETROFIT)
Retrofit provideDefault();
@Named(DaggerConstants.OTHER_RETROFIT)
Retrofit provideOther();
void inject(MainActivity activity);
}
模块:
@Module
public class NetworkModule {
@Named(DaggerConstants.DEFAULT_RETROFIT)
@PerApp
@Provides
Retrofit provideDefaultRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.someurl.com/")
.build();
return retrofit;
}
@Named(DaggerConstants.OTHER_RETROFIT)
@PerApp
@Provides
Retrofit provideOtherRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.someotherurl.com/")
.build();
return retrofit;
}
@PerApp
@Provides
SharedPreferences networkPreferences(Context context) {
return context.getSharedPreferences("network", Context.MODE_PRIVATE);
}
}
我有一些问题:
1) 我将这两个组件存储在 Android 中的应用程序中。
但对我来说,存储 AppComponent 和 NetworkComponent 似乎很奇怪。
我的 ApplicationComponent 应该提供 NetworkComponent 不是更好吗?
2) 注释 @PerApp 和其他东西是不是意味着什么,或者 Dagger 只是在寻找一个有 @PerApp 注释的对象,如果没有,它会删除它?这个我不清楚。
3) 用 @Singleton 标记模块是否有用,因为这是可能的,但我在任何示例中都没有看到。
I store the two components in the Application in Android. But to me it seems strange that I store the AppComponent and the NetworkComponent. Is it not better that my ApplicationComponent should provide the NetworkComponent?
您缺少组件的概念。组件背后的想法是拥有对象,它们的生命周期彼此不同。
例如:
- 对象
A
应该是应用单例。无论何时你需要这个对象,它都是完全相同的对象
- 对象
B
应该是 activity 单例。每次您的 activity 销毁并创建一个新对象时,都会创建一个新对象。
对象C
应该是片段单例。每次您的片段附加和分离到 activity 时,都会创建一个新对象。
因此您指定,您希望 ComponentC
具有 ComponentB
的依赖性,而 ComponentB
具有对 ComponentA
的依赖性(假设它们中的每一个都提供了适当的命名依赖性。)
The annotations @PerApp
and stuff does it mean something or is Dagger just looking that there is an Object a live that has the @PerApp annotation and if not then it deletes that? This is not clear to me.
自定义范围对您负责的对象很有用。即,如果您已使用自定义范围声明了一个组件,则您有责任清除该组件,此后下次从组件请求依赖项时,将为您创建一个新对象。
在上面的例子中,一旦你的 activity 被销毁,你必须注意清空 ComponentB
,否则下次它会为你提供相同的 B
对象你自求。
Is it something useful to mark a Module with for example @Singleton because it is possible but I don't see that in any example.
提供依赖关系不会有任何区别。但也许它会帮助您记住托管组件的范围。
我正在试验 Dagger 2,我只是在测试一些东西以了解框架。
我有一个 ApplicationComponent 需要是整个应用程序的单例,所以我这样定义它:
@Component(modules = {ApplicationModule.class})
@Singleton
public interface ApplicationComponent {
Context provideContext();
}
含模块:
@Module
public class ApplicationModule {
private Application appContext;
public ApplicationModule(Application appContext) {
this.appContext = appContext;
}
@Provides
@Singleton
public Context provideContext() {
return appContext;
}
}
现在我还想要一个 NetworkComponent,它需要在应用程序运行期间一直运行。 该网络组件需要依赖于 ApplicationComponent。 所以我的网络组件如下:
@Component(dependencies = {ApplicationComponent.class}, modules = {NetworkModule.class})
@PerApp
public interface NetworkComponent extends ApplicationComponent {
@Named(DaggerConstants.DEFAULT_RETROFIT)
Retrofit provideDefault();
@Named(DaggerConstants.OTHER_RETROFIT)
Retrofit provideOther();
void inject(MainActivity activity);
}
模块:
@Module
public class NetworkModule {
@Named(DaggerConstants.DEFAULT_RETROFIT)
@PerApp
@Provides
Retrofit provideDefaultRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.someurl.com/")
.build();
return retrofit;
}
@Named(DaggerConstants.OTHER_RETROFIT)
@PerApp
@Provides
Retrofit provideOtherRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.someotherurl.com/")
.build();
return retrofit;
}
@PerApp
@Provides
SharedPreferences networkPreferences(Context context) {
return context.getSharedPreferences("network", Context.MODE_PRIVATE);
}
}
我有一些问题:
1) 我将这两个组件存储在 Android 中的应用程序中。 但对我来说,存储 AppComponent 和 NetworkComponent 似乎很奇怪。 我的 ApplicationComponent 应该提供 NetworkComponent 不是更好吗?
2) 注释 @PerApp 和其他东西是不是意味着什么,或者 Dagger 只是在寻找一个有 @PerApp 注释的对象,如果没有,它会删除它?这个我不清楚。
3) 用 @Singleton 标记模块是否有用,因为这是可能的,但我在任何示例中都没有看到。
I store the two components in the Application in Android. But to me it seems strange that I store the AppComponent and the NetworkComponent. Is it not better that my ApplicationComponent should provide the NetworkComponent?
您缺少组件的概念。组件背后的想法是拥有对象,它们的生命周期彼此不同。 例如:
- 对象
A
应该是应用单例。无论何时你需要这个对象,它都是完全相同的对象 - 对象
B
应该是 activity 单例。每次您的 activity 销毁并创建一个新对象时,都会创建一个新对象。 对象
C
应该是片段单例。每次您的片段附加和分离到 activity 时,都会创建一个新对象。因此您指定,您希望
ComponentC
具有ComponentB
的依赖性,而ComponentB
具有对ComponentA
的依赖性(假设它们中的每一个都提供了适当的命名依赖性。)
The annotations
@PerApp
and stuff does it mean something or is Dagger just looking that there is an Object a live that has the @PerApp annotation and if not then it deletes that? This is not clear to me.
自定义范围对您负责的对象很有用。即,如果您已使用自定义范围声明了一个组件,则您有责任清除该组件,此后下次从组件请求依赖项时,将为您创建一个新对象。
在上面的例子中,一旦你的 activity 被销毁,你必须注意清空 ComponentB
,否则下次它会为你提供相同的 B
对象你自求。
Is it something useful to mark a Module with for example @Singleton because it is possible but I don't see that in any example.
提供依赖关系不会有任何区别。但也许它会帮助您记住托管组件的范围。