1

我有一个具有许多类似分片对象的非 Android 应用程序,并且我希望每个分片(数据库客户端、DAO ......)中的所有对象都是单例的。

为此,我创建了一个 ShardSingleton 注释:

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ShardSingleton

我在自己的范围内创建每个分片对象:

    var shard1: Shard = KTP.openScopes("app", "shard1")
        .supportScopeAnnotation(ShardSingleton::class.java)
        .getInstance(Shard::class.java)

为了让我的 DAO 实际上成为其分片中的单例,我必须同时使用@ShardSingleton和注释它@Singleton

@Singleton // the FooDAO is not a singleton without this annotation
@ShardSingleton
@InjectConstructor
class FooDAO(val dbClient: DBClient)

乍一看(可能出于无知),我认为@ShardSingleton就足够了。

是预期的吗?

这是演示该行为的要点: https ://gist.github.com/bfreuden/a866b21c5a6342a3ce1ed26aa636f9f6

4

0 回答 0