injected 类 如何获得对注入器的引用?
How do injected classes get a reference to the injector?
我最近开始在一个小型 Android 项目中使用 Dagger 2。我不确定我应该在哪里构建我的 @Component
.
假设我有一个 @Module
提供的依赖项又依赖于 Application
。显然,您无法实例化 @Module
,因此无法在不引用 Application
的情况下构建 @Component
。在那种情况下,Application
本身构建并保存对 @Component
的引用是否有意义,然后哪些活动和片段可以获得注入自己?换句话说,而不是这个:
MyComponent component = DaggerMyComponent.builder()
.myModule(new MyModule((MyApp) getApplication()))
.build();
component.inject(this);
活动会这样做:
((MyApp) getApplication()).getMyComponent().inject(this);
第二种方式有什么缺点吗?而如果模块提供@Singleton
依赖,是否有必要采用第二种方式?
编辑:我写了一个非Android测试程序。如我所料,@Component
接口的不同实例会产生不同的 @Singleton
资源实例。所以看来我最后一个问题的答案是肯定的,除非有其他机制让 @Component
本身成为一个单例。
final AppComponent component1 = DaggerAppComponent.create();
final AppComponent component2 = DaggerAppComponent.create();
System.out.println("same AppComponent: " + component1.equals(component2)); // false
// the Bar producer is annotated @Singleton
System.out.println("same component, same Bar: " + component1.bar().equals(component1.bar())); // true
System.out.println("different component, same Bar: " + component1.bar().equals(component2.bar())); // false
您的组件必须位于界面中。假设您有一个这样的模块
@Module
public class MainActivityModule {
@Provides
public Gson getGson(){
return new Gson();
}
}
现在您想为这个模块创建一个接口,以便您可以在 activity 中使用它。我将 activity 注入到这个界面中,但是当你想用于许多其他活动时这会很棘手,所以现在我们只说你想使用 MainActivity
@Component(
modules = MainActivityModule.class) //The module you created
public interface IAppModule {
void inject(MainActivity activity);
}
现在您可以在 MainActivity 中使用,但首先要构建项目,因为 Dagger2 需要根据您创建的模块和组件创建自己的 classes。注意你没有制作classDaggerIAppModule
它是你建工程后创建的
IAppModule appComponent;
@Inject
Gson gson;
public void setupDaggerGraph(){ //call this method in your onCreate()
appComponent = DaggerIAppModule.builder()
.mainActivityModule(new MainActivityModule())
.build();
appComponent.inject(this);
}
你的建议是正确的。 @Singleton
组件在其生命周期内仅保证 @Singleton
范围内的事物的一个实例,因此您的应用程序必须保留该组件。
我最近开始在一个小型 Android 项目中使用 Dagger 2。我不确定我应该在哪里构建我的 @Component
.
假设我有一个 @Module
提供的依赖项又依赖于 Application
。显然,您无法实例化 @Module
,因此无法在不引用 Application
的情况下构建 @Component
。在那种情况下,Application
本身构建并保存对 @Component
的引用是否有意义,然后哪些活动和片段可以获得注入自己?换句话说,而不是这个:
MyComponent component = DaggerMyComponent.builder()
.myModule(new MyModule((MyApp) getApplication()))
.build();
component.inject(this);
活动会这样做:
((MyApp) getApplication()).getMyComponent().inject(this);
第二种方式有什么缺点吗?而如果模块提供@Singleton
依赖,是否有必要采用第二种方式?
编辑:我写了一个非Android测试程序。如我所料,@Component
接口的不同实例会产生不同的 @Singleton
资源实例。所以看来我最后一个问题的答案是肯定的,除非有其他机制让 @Component
本身成为一个单例。
final AppComponent component1 = DaggerAppComponent.create();
final AppComponent component2 = DaggerAppComponent.create();
System.out.println("same AppComponent: " + component1.equals(component2)); // false
// the Bar producer is annotated @Singleton
System.out.println("same component, same Bar: " + component1.bar().equals(component1.bar())); // true
System.out.println("different component, same Bar: " + component1.bar().equals(component2.bar())); // false
您的组件必须位于界面中。假设您有一个这样的模块
@Module
public class MainActivityModule {
@Provides
public Gson getGson(){
return new Gson();
}
}
现在您想为这个模块创建一个接口,以便您可以在 activity 中使用它。我将 activity 注入到这个界面中,但是当你想用于许多其他活动时这会很棘手,所以现在我们只说你想使用 MainActivity
@Component(
modules = MainActivityModule.class) //The module you created
public interface IAppModule {
void inject(MainActivity activity);
}
现在您可以在 MainActivity 中使用,但首先要构建项目,因为 Dagger2 需要根据您创建的模块和组件创建自己的 classes。注意你没有制作classDaggerIAppModule
它是你建工程后创建的
IAppModule appComponent;
@Inject
Gson gson;
public void setupDaggerGraph(){ //call this method in your onCreate()
appComponent = DaggerIAppModule.builder()
.mainActivityModule(new MainActivityModule())
.build();
appComponent.inject(this);
}
你的建议是正确的。 @Singleton
组件在其生命周期内仅保证 @Singleton
范围内的事物的一个实例,因此您的应用程序必须保留该组件。