Answer the question
In order to leave comments, you need to log in
How to parameterize @Qualifier in JavaEE CDI?
I have a service that provides access to a MongoDB client:
// MongoService.java
public interface MongoService {
public MongoClient getClient();
}
// MongoServiceImpl.java
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class MongoServiceImpl implements MongoService {
MongoClient client = null;
@PostConstruct
public void init() {
client = new MongoClient();
}
@Override
@Lock(LockType.READ)
public MongoClient getClient() {
return client;
}
}
//Mongo.java
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Mongo {
@Nonbinding String database() default "default";
@Nonbinding String collection() default "";
}
// MongoProducer.java
@Stateless
public class MongoProducer {
@EJB
MongoService mongoService;
@Produces
@Mongo
public MongoDatabase getDatabase(InjectionPoint injectionPoint) {
Mongo annotation = injectionPoint.getAnnotated().getAnnotation(Mongo.class);
return mongoService.getClient().getDatabase(annotation.database());
}
@Produces
@Mongo
public MongoCollection<Document> getCollection(InjectionPoint injectionPoint) {
Mongo annotation = injectionPoint.getAnnotated().getAnnotation(Mongo.class);
String collectionName = annotation.collection();
if (Objects.equals(collectionName, "")) {
return null;
}
return mongoService.getClient().getDatabase(annotation.database()).getCollection(collectionName);
}
}
org.bson.Document
, but you need to be able to use your own type. @Mongo
and class MongoProducer
so that you can use your document type like this:@Stateless
public class FooBean {
@Inject
@Mongo(collection = "foo", documentType = MyDocument.class)
MongoCollection<MyDocument> collection;
...
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question