Dynamically fire CDI event with qualifier with members

前端 未结 2 1712
猫巷女王i
猫巷女王i 2021-01-12 15:29

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 annotat

相关标签:
2条回答
  • 2021-01-12 15:41

    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;
    }
    
    }
    

    then just fire like this:

    dynamicEventFirer.fireEvent(new TypeQualifier(TypeEnum.TYPEA));
    
    0 讨论(0)
  • 2021-01-12 15:57

    You need to declare an abstract TypeQualifier that extends AnnotationLiteral and implements Type

    abstract class TypeQualifier extends AnnotationLiteral<Type> implements Type{}
    

    and use it like this

    dynamicEventFirer.fireEvent(new TypeQualifier() {
    public TypeEnum value() {
        return TypeEnum.TYPEA;
    }
    });
    

    and later if you want to fire an event with TypeEnum.TYPEB

    dynamicEventFirer.fireEvent(new TypeQualifier() {
    public TypeEnum value() {
        return TypeEnum.TYPEB;
    }
    });
    
    0 讨论(0)
提交回复
热议问题