- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个通过插入“.class”链接到另一个库的类,所以我不知道如何在这个类中注入(inject)依赖项,因为我不是创建对象的人和库只是用默认构造函数实例化类。
例子:
这是 Room 的转换器类
public class AttendeeListConverter {
@TypeConverter
public List<Attendee> fromJson(String attendeesJson) {
if (attendeesJson == null) {
return null;
}
return gson.fromJson(attendeesJson, new TypeToken<List<Attendee>>() { }.getType());
}
}
我想注入(inject)我的 Gson 单例实例,但我不是创建该转换器类对象的人,我只是传递“AttendeesListConverter.class”,对象本身的创建是代码生成。
最佳答案
如果您既不控制对象的创建,也不在事后访问对象实例,我认为没有一个干净的解决方案。
鉴于这些情况,您唯一可以做的就是从对象中获取一个组件并让它注入(inject)自身。
class Injector {
public static AppComponent component;
}
class AttendeeListConverter {
public AttendeeListConverter() {
AppComponent component = Injector.component
component.inject(this);
}
}
// and initialize it in onCreate of your Application
class MyApp extends Application {
@Inject AppComponent appComponent;
onCreate() {
// ...setup...
Injector.component = appComponent
}
}
您当然也可以将组件静态存储在应用程序本身中,或者有些人还喜欢将应用程序存储在静态变量中,这样您就可以使用 MyApp.appComponent.inject()
相反。
关于android - Dagger 2 - 如何注入(inject)一个我不控制其对象实例化的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640655/
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
我是一名优秀的程序员,十分优秀!