- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我遇到了与 this 中类似的问题问题。虽然接受的答案确实有帮助,但我缺少解决问题的最后一部分。
我有 2 个 android 库模块:common
和 exp
,它依赖于 common
。
common
下的所有内容:
@Module
public class CommonModule {
@Singleton
@Provides
public Repository providesRepository() {
return new Repository();
}
}
@Singleton
@Component(modules={CommonModule.class})
public interface CommonComponent {
void inject(CommonClass commonClass);
/**
CommonClass needs instance of Repository
**/
}
public class CommonDIHolder {
public static CommonComponent sComponent;
public static void init() {
sComponent = DaggerCommonComponent.builder().build();
}
}
exp
下的所有内容:
@Module(includes={CommonModule.class})
public class ExpModule {
@Singleton
@Provides
public ExpResource provideExpResource() {
return new ExpResource();
}
}
@Singleton
@Component(modules={ExpModule.class}, dependencies={CommonComponent.class})
public interface ExpComponent {
void inject(ExpClass expClass);
/**
ExpClass needs instance of Repository and ExpResource
**/
}
public class ExpDIHolder {
public static ExpComponent sComponent;
public static void init() {
sComponent = DaggerExpComponent.builder()
.commonComponent(CommonDIHolder.sComponent)
.build();
}
}
我需要 CommonClass
和 ExpClass
接收相同的 Repository
实例。
这种方法的问题是 @Singleton
不能依赖于 @Singleton
。所以我必须将 ExpComponent
的范围更改为名为 @ExpScope
的自定义范围。然后我也将 provideExpResource
更改为 @ExpScope
。
然后我遇到了一个错误,说 ExpComponent
不能引用不同范围的绑定(bind)。它指的是 provideRepository
,它具有不同的作用域 (@Singleton
)。如果我将范围更改为 ExpScope
,则 CommonComponent
将具有与 provideRepository
不同的范围。
如果我将所有 @Singleton
更改为 @ExpScope
然后我收到此错误消息:depends on scoped components in a non-hierarchical scope ordering
我该怎么办?或者我在这里采用了错误的方法?
最佳答案
只使用一个@Singleton
作用域组件
你应该有一个并且只有一个 @Singleton
作用域组件,如下所示:
@Singleton
@Component(modules={CommonModule.class, ExpModule.class})
public interface CommonComponent {
}
仅将 Activity 、 fragment 和服务指定为组件的显式注入(inject)目标
在 Android 应用程序中,您应该只将 Activity、Fragments 和 Services 列为注入(inject)站点。您应该配置 Dagger 2 以注入(inject)其余的依赖项,而不必求助于在它们内部调用 component.inject(this)
。
例如,如果您的 CommonClass
如下所示:
public class CommonClass {
@Inject Repository repository;
public class CommonClass() {
CommonComponentHolder.get().inject(this);
}
}
像这样重构它:
public class CommonClass {
private final Repository repository;
@Inject
public class CommonClass(Repository repository) {
this.repository = repository;
}
}
现在,当您有一个需要 CommonClass
的 Activity 或 Fragment 并且您正在注入(inject) CommonComponent
或其子组件或依赖组件之一时,它们可以获得CommonClass
连接了正确的依赖项:
public class MyActivity extends AppCompatActivity {
@Inject CommonClass commonClass;
public void onCreate(Bundle savedInstanceState) {
CommonComponentHolder.getComponent().inject(this);
}
}
使用子组件或依赖组件指定注入(inject)目标
现在您有了一个 @Singleton
作用域组件,您可能想要为您的 Activity 或 Fragment 创建一个更窄作用域的组件。您必须将它连接到您的 CommonComponent
,因此使用依赖组件或子组件(从 Dagger 2.10 开始首选子组件)。既然你说你已经尝试定义一个 @ExpScope
,我认为缺少的部分是使用注入(inject)你的 Activity 或 Fragment 的 @ExpScope
制作子组件或依赖组件。
顶级单例组件类似于以下内容:
@Singleton
@Component(modules={CommonModule.class, ExpModule.class})
public interface CommonComponent {
ExpComponent.Builder exComponent();
}
然后对于子组件:
@ExpScope
@Subcomponent(modules = {NarrowerScopedModule.class})
public interface ExpComponent {
@Subcomponent.Builder
public interface Builder {
Builder narrowerScopedModule(NarrowerScopedModule narrowerScopedModule);
ExpComponent build();
}
}
Google Android Architecture Blueprints Github repo 中有很好的 Android 项目示例。
关于android - Dagger - 在不同的组件上获取相同的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953373/
Dagger 和 Dagger 2.0 有什么区别,为什么 Google 决定 fork 现有项目? 最佳答案 来自 Dagger 2 presentation 的一些引述 Dagger 1 的问题:
我想制作一个包含一个主要 Activity 和多个 fragment 的简单项目。在这里,我在一项 Activity 中有两个 fragment ,我想将演示者注入(inject)登录 fragmen
我是 dagger 的新手,我正在寻找如何在 dagger-2.x 中实现 spring 配置文件等功能。我想为我的开发环境和生产环境使用不同的 bean,但我使用的是带有 Java 的 Dagger
假设我有两个服务 AService 和 BService,它们都需要一个 API key 。 所以在我的模块中,我不能做类似的事情: @Provides @Singleton @A @ApiKey S
我对 Dagger 很陌生——我什至不知道它是否适用于我的应用程序 我有一个搜索页面,它返回有关给定名人的最新消息。 我写了一个测试来验证当我们搜索一个受欢迎的名人时结果会出现在页面上。 page有一
我最近将 dagger 2.8 更新为 2.9 dagger。和最新版本的文档已添加如下: -添加@BindsInstance组件构建器可以轻松绑定(bind)在图之外构建的实例。 -制作人:已添加
Dagger documentation页面说: To get the most out of compile-time validation, create a module that includ
我可以知道之间的区别吗? @Singleton VS 静态 在dagger2中提供? @Provides static User currentUser(AuthManager authManager
我有一个功能,我需要为不同的用户提供不同的房间数据库。 我正在使用 Dagger 2 创建房间数据库。我的应用程序组件创建一个房间数据库。当用户切换到另一个用户时,我想为此创建新的房间数据库,我需要创
我是 Dagger 2 的新手,正在尝试 IntelliJ 中的 Dagger 2 Coffee Example,它似乎不会生成 DaggerCoffeeApp_Coffee(它应该生成它),即使我密
我正在尝试在 Android 上使用 Dagger 2。我以前让它工作,我有一个 appModule 将依赖项注入(inject)应用程序中的特定类。我的问题是我收到错误 Error:(14, 55)
我是 kotlin 和 Dagger 的新手。我有一个小问题,我不知道如何解决,也没有找到解决方案。 这就是我所拥有的: @Module class AppModule (app: Applicati
我正在尝试使用 Dagger 建立一个项目。现在,当我构建时,没有为组件生成任何 Dagger* 类,并且出现以下错误: Error:(10, 8) error: [com.redditapp.dag
在 Dagger 中,我有时会看到有些组件只扩展一个接口(interface),而其他组件则使用 dependencies。 例如我们有一个基础组件: @Singleton @Component(mo
有没有办法告诉 Dagger 2 如何提供一些东西,但不允许它被注入(inject)? 假设我要注入(inject)一个 Qux .一个 Qux需要 Foo和 Bar : @Provides @Sin
( x-post from /r/androiddev ) 我只想在序言中说这不是一篇“哪个更好”的帖子;这严格来说是一个关于如何使用 Dagger 构建某些东西的问题(以及如何在 Kodein 中构
https://developer.android.com/topic/libraries/architecture/ 在Android架构蓝图中,为什么dagger是基于mvp架构而不是MVVM架构
我第一次开始同时使用 Kotlin 和 Dagger 2。我假设一切都与 Java 中的相同,但显然不完全相同。 Dagger 不会为我生成 Dagger* 文件。这是我的代码: 组件: @PerAc
我正在尝试将 Dagger 2 与 eclipse 集成,我使用的库如下: dagger-2.0.jar dagger-compiler-2.0.jar guava-13.0.1.jar javawr
我想练习这个Dagger 2 Vehicle Motor例子。 除了我的 gradel.build 之外,我所做的一切都与该教程中的一样: compile 'com.google.dagger:dag
我是一名优秀的程序员,十分优秀!