I have a problem with bean injections in Quarkus, for MongoClient.
I need a custom MongoClient with a custom Codec:
@ApplicationScoped
public class MongoClientProducer {
String connectionString = "mongodb+srv://...";
@Produces
@MongoClientName("customClient")
public MongoClient createMongoClient() {
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(new CustomCodec()),
MongoClientSettings.getDefaultCodecRegistry()
);
MongoClientSettings settings = MongoClientSettings.builder()
.codecRegistry(codecRegistry)
.applyConnectionString(new ConnectionString(connectionString))
.build();
return MongoClients.create(settings);
}
}
Then I use it:
@ApplicationScoped
public class MyService implements SomeInterface{
@Inject
@MongoClientName("customClient")
MongoClient customMongoClient;
...
}
And I get the following error:
jakarta.enterprise.inject.AmbiguousResolutionException: Ambiguous dependencies for type com.mongodb.client.MongoClient and qualifiers [@MongoClientName("customClient")]s>
- injection target: com.myproject.MyService#customMongoClient
- declared on CLASS bean [types=[com.myproject.SomeInterface, java.lang.Object, com.myproject.MyService], qualifiers=[@Default, @Any], target=com.myproject.MyService]
- available beans:
- PRODUCER METHOD bean [types=[java.lang.AutoCloseable, com.mongodb.client.MongoClient, java.io.Closeable, java.lang.Object], qualifiers=[@Any, @MongoClientName("customClient")], target=com.mongodb.client.MongoClient createMongoClient(), declaringBean=ccom.myproject.MongoClientProducer]
- SYNTHETIC bean [types=[com.mongodb.client.MongoClient, java.lang.Object], qualifiers=[@Any, @Named("customClient"), @io.quarkus.mongodb.MongoClientName("customClient")], target=n/a]
I don’t understand why I got twice the bean with MongoClientName(“customClient”), in producer and synthetic.
I expect having only a single bean for my MongoClient.
I’ve tried to remove my mongo dependancies in the pom.xml, to see some of theme were redundant.