我尝试在我的项目中使用 Dagger2。我有一个 Firebase 服务和一个名为 SyncFactory 的类,它发出一个特定的请求。当我接到 Firebase 的电话时,我提出了我的请求。我创建了一个经理模块
@Module(includes = [RepositoryModule::class, NetworkModule::class, AppModule::class])
class ManagersModule {
...
@Singleton
@Provides
fun provideSyncFactory(context: Context, accountsRepository: AccountsRepository, messagesRepository: MessagesRepository) : SyncFactory {
return SyncFactory(context, accountsRepository, messagesRepository)
}
...
}
SyncFactory 类如下所示
class SyncFactory @Inject constructor(
private val context: Context,
private val accountsRepository: AccountsRepository,
private val messagesRepository: MessagesRepository
) {
fun getAccounts(){....}
我也有一个界面
@Singleton
@Component(modules = [ViewModelsModule::class, DatabaseModule::class, RepositoryModule::class, AppModule::class, NetworkModule::class, ManagersModule::class])
interface ViewModelComponent {
fun inject(viewModels: ViewModels)
fun inject(firebaseService: AppFirebase)
}
最后在我的 firebase 服务中,我注入了 SyncFactory
class AppFirebase : FirebaseMessagingService(), SyncFactoryCallback {
@Inject
lateinit var syncFactory: SyncFactory
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
// lateinit property syncFactory has not been initialized
syncFactory.getAccounts()
}
当我的服务被调用时,我得到一个 lateinit 属性 syncFactory has not been initialized 异常。我做错了什么..?