Skip to content

The order of dependencies with @Qualifier annotation modifies the scope of the dependency. #424

@AdriaBosch

Description

@AdriaBosch

I have found the following behavior related to scopes and @Qualifier annotation. Suppose we have the component:

@Scope
annotation class Singleton

@Qualifier
annotation class FirstClient

@Qualifier
annotation class SecondClient

interface ClientComponent {

    @Singleton
    @Provides
    @FirstClient
    fun provideFirstClient(): HttpClient {
        return HttpClient()
    }

    @Singleton
    @Provides
    @SecondClient
    fun provideSecondClient(
        @FirstClient
        firstClient: HttpClient,
    ): HttpClient {
        return firstClient.newBuilder().build()
    }
}

If we create this Api class with this particular order of dependencies

@Inject
class Api(
    @SecondClient
    val secondClient: HttpClient,

    @FirstClient
    val firstClient: HttpClient,
)

then FirstClient is not scoped and two instances are created. On the other hand if we create the same class but with different dependency order:

@Inject
class Api(
    @FirstClient
    val firstClient: HttpClient,

    @SecondClient
    val secondClient: HttpClient,
)

then FirstClient gets its scope and only one instance is created.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions