Dagger每次从Singleton类创建新实例

问题描述 投票:0回答:1

假设我有一个使用匕首注入的Singleton类

@Singleton
class SingletonClass {

    @Inject
    lateinit var newInstanceEveryTime: NewInstanceEveryTime

    fun getNewInstance(): NewInstanceEveryTime {
        return newInstanceEveryTime
    }
}

[每当我使用SingletonClass.getNewInstance()调用方法getNewInstance()时,我都需要每次获取新实例,但是我知道它不会每次都创建该实例,有人可以帮助我如何从中实现这一点。使用匕首的单例类。

android dependency-injection dagger-2 dagger
1个回答
-1
投票
或者您可以这样做:

@Singleton class SingletonClass { fun getNewInstance() = NewInstanceEveryTime() }

但是您的用例是什么?为什么需要这种工厂方法?
© www.soinside.com 2019 - 2024. All rights reserved.