使用带有成员的限定符动态触发CDI事件 [英] Dynamically fire CDI event with qualifier with members

查看:86
本文介绍了使用带有成员的限定符动态触发CDI事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JBoss AS6上的后端服务中使用CDI事件-理想情况下具有最大的代码重用性。

I'm trying to use CDI events in my backend services, on JBoss AS6 - ideally with maximum code reuse.

我可以从可以剪切的文档中看到我必须使用带有成员的限定词来创建限定词注释类,例如

I can see from the docs I can cut down on the qualifier annotation classes I have to create by using a qualifier with members e.g.

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface Type {
    TypeEnum value();
}

我可以通过

public void onTypeAEvent(@Observes @Type(TypeEnum.TYPEA) String eventMsg) {...}

到目前为止,很好。但是,为了进一步减少所需的类数,我想拥有一个EventFirer类,该类中抛出的事件的限定符是动态的。没有成员的限定词没有问题:

So far, so good. However, to further cut down on the number of classes needed, I want to have one EventFirer class, where the qualifier of the event thrown is dynamic. Not a problem with qualifiers without members:

public class DynamicEventFirer {

    @Inject @Any private Event<String> event;

    public void fireEvent(AnnotationLiteral<?> eventQualifier){
        event.select(eventQualifier).fire("FIRED"); 
    }
}

然后称为

dynamicEventFirer.fireEvent(new AnnotationLiteral<Type>() {});

但是什么时候限定词应该有成员呢?查看AnnotationLiteral的代码,当然是为成员设置的,并且类元素注释具有示例:

But what about when the qualifier should have members? Looking at the code for AnnotationLiteral, it's certainly setup for members, and the class element comment has the example:

new PayByQualifier() { public PaymentMethod value() { return CHEQUE; } }

这对我来说很有意义-您正在覆盖注释接口的value()方法。但是,当我自己尝试以下操作时:

This makes sense to me - you're overriding the value() method of the annotation interface. However, when I tried this myself:

dynamicEventFirer.fireEvent(new AnnotationLiteral<Type>() {
    public TypeEnum value() {
        return TypeEnum.TYPEA;
    }
});

我收到例外情况

java.lang.RuntimeException: class uk.co.jam.concept.events.MemberQualifierEventManager$1 does not implement the annotation type with members uk.co.jam.concept.events.Type
    at javax.enterprise.util.AnnotationLiteral.getMembers(AnnotationLiteral.java:69)
    at javax.enterprise.util.AnnotationLiteral.hashCode(AnnotationLiteral.java:281)
    at java.util.HashMap.getEntry(HashMap.java:344)
    at java.util.HashMap.containsKey(HashMap.java:335)
    at java.util.HashSet.contains(HashSet.java:184)
    at org.jboss.weld.util.Beans.mergeInQualifiers(Beans.java:939)
    at org.jboss.weld.bean.builtin.FacadeInjectionPoint.<init>(FacadeInjectionPoint.java:29)
    at org.jboss.weld.event.EventImpl.selectEvent(EventImpl.java:96)
    at org.jboss.weld.event.EventImpl.select(EventImpl.java:80)
    at uk.co.jam.concept.events.DynamicEventFirer.fireEvent(DynamicEventFirer.java:20)

有人可以看到我在做什么吗? MemberQualifierEventManager是一个ApplicationScoped Bean,它调用DynamicEventFirer触发事件。

Can anyone see what I'm doing wrong? MemberQualifierEventManager is an ApplicationScoped bean which calls on DynamicEventFirer to fire the event.

谢谢,
Ben

Thanks, Ben

推荐答案

根据您的帖子,有一种更简洁的方法:

There's a slightly cleaner way to do it based on your post:

public class TypeQualifier extends AnnotationLiteral<Type> implements Type{

private TypeEnum type;

public TypeQualifier(TypeEnum t) {
      this.type = t;
}

public TypeEnum value() {
    return type;
}

}

然后就这样射击:

dynamicEventFirer.fireEvent(new TypeQualifier(TypeEnum.TYPEA));

这篇关于使用带有成员的限定符动态触发CDI事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆