'Getting Ambiguous dependency warning when creating Producer method

I've created a producer method to handle my authentication to Azure using Managed Identities. My application uses multiple Azure services, so I thought this might be the best way to just have one producer method for the authentication of all my Azure services.

My producer method looks like this:

import com.azure.core.credential.TokenCredential
import com.azure.identity.ManagedIdentityCredentialBuilder
import io.quarkus.arc.properties.IfBuildProperty
import javax.enterprise.context.ApplicationScoped
import javax.enterprise.inject.Produces

@ApplicationScoped
class AzureManagedIdentity(
    private val azureManagedIdentityConfig: AzureManagedIdentityConfig,
) {
    @Produces
    fun managedIdToken(): TokenCredential = ManagedIdentityCredentialBuilder()
        .clientId(azureManagedIdentityConfig.clientId())
        .build()
}

This seems to work, but I'm getting the following warning in my IDE: "Ambiguous dependency: there are multiple beans that match the injection point".

I'm using the TokenCredential (e.g. way of authenticating) like this:

import javax.enterprise.context.ApplicationScoped
import com.azure.core.credential.TokenCredential

@ApplicationScoped
class ServiceBusCaptureRequestPublisher(
    private val tokenCredential: TokenCredential,
    ) {
    // Logic
}

I searched the interwebs and found something about the code not knowing which TokenCredential needs to be used, or something like it. Now I also found something about Qualifiers, and the @Named annotation, but I'm a bit at a loss here.

Does anyone know how I can resolve this?



Solution 1:[1]

  • Have you gone through the Bean types in your deployment. Which will help you in resolving Ambitious Resolution.
  • @Typed annotation is available since CDI 1.0
  • You could rely on qualifiers as well though it is preferable to use qualifiers for functional semantic.
  • Also check these restricting bean types.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SaiSakethGuduru-MT