[英]How to create generic producer for qualified and scoped CDI beans?
I am trying to do something like the following: 我正在尝试执行以下操作:
@Qualifier
@Retention(RUNTIME)
@Target({ PARAMETER, FIELD, METHOD, TYPE })
public @interface ForQueueName{
String value();
}
public class JmsSenderProducer {
@Produces
@Any
@ApplicationScoped
public JmsSender createJmsSender(InjectionPoint ip) {
ForQueueName annotation = ip.getAnnotated().getAnnotation(ForQueueName.class);
return new JmsSender(annotation.value());
}
}
// Somewhere else
@Inject
@ForQueueName("java:/jms/queue/my.queue.name")
JmsSender myQueueSender;
Of course this does not work because 当然这是行不通的,因为
@Any
does not work as a replacement for any qualified @ForQueueName
@Any
不能代替任何合格的@ForQueueName
InjectionPoint
cannot be used when producing @ApplicationScoped
bean. 产生@ApplicationScoped
bean时不能使用InjectionPoint
。 I know I could create @Dependent
scoped beans with a nonbinding qualifier this way. 我知道我可以使用非绑定限定符来创建@Dependent
范围内的bean。 But in my case I actually need a non dependent scoped bean. 但就我而言,我实际上需要一个非依赖范围的bean。
Is something like the intended possible? 有可能达到预期的目标吗? If not, is there a particular reason why? 如果不是,是否有特定原因?
Cheers, Tilmann 蒂尔曼干杯
EDIT: Fixed scope name from @Default
to @Dependent
编辑:固定范围名称从@Default
到@Dependent
First, you should consider using JMSContext
in your app. 首先,您应该考虑在应用程序中使用JMSContext
。 Its a JMS 2.0 feature with a much cleaner API. 它的JMS 2.0功能具有更简洁的API。
Second, your producer method should be annotated with ForQueueName
. 其次,您的生产者方法应使用ForQueueName
进行注释。 For that to work, the value()
attribute of ForQueueName
needs to be marked as @Nonbinding
. 为此, ForQueueName
的value()
属性需要标记为@Nonbinding
。 This tells the CDI container that the value should not be considered when looking up the producer. 这告诉CDI容器,在查找生产者时不应考虑该值。
@Qualifier
@Retention(RUNTIME)
@Target({ PARAMETER, FIELD, METHOD, TYPE })
public @interface ForQueueName{
@Nonbinding String value();
}
public class JmsSenderProducer {
@Produces
@ForQueueName("")
@Dependent // must be dependent to interrogate the injection point
public JmsSender createJmsSender(InjectionPoint ip) {
ForQueueName annotation = ip.getAnnotated().getAnnotation(ForQueueName.class);
return new JmsSender(annotation.value());
}
}
// Somewhere else
@Inject
@ForQueueName("java:/jms/queue/my.queue.name")
JmsSender myQueueSender;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.