1
votes

I have a abstract super class:

public abstract class A<T extends Other> extends B {...}

Another class defined with @ViewScoped and @Named is extending A:

public class C extends A<OtherSub> implements Serializable {...}

So far this is no problem.

Now I would like to add observer methods in the super class A to generically handle CDI events, e.g.:

public void entityCreated(@Observes(notifyObserver = Reception.IF_EXISTS) @Created EntityChangedEvent event) {...}

But deploying this wildfly throws an exception during deployment:

WELD-000404: Conditional observer method cannot be declared by a @Dependent scoped bean: [BackedAnnotatedMethod] public A.entityCreated(@Observes @Created EntityChangedEvent)

I know that I can create the observer without the notifyObserver condition, but this has the effect, that other beans will be created and notified upon the CDI event, which I would like to prevent.

Is there any way to use the notifyObserver condition in the generic super class? Or do I need to use it without that condition respectively implement it in the concrete subclass?

1

1 Answers

2
votes

Just looking at CDI 1.2 spec, section 10.4.4 Conditional observer methods:

Beans with scope @Dependent may not have conditional observer methods.

Therefore your request is invalid in this regard and it cannot work.One of the reasons I can glance for this is that @Dependent beans are not normal scoped and basically can exist "within given context" (note the quotes) 0-n times. Hence you would have some ambiguity in notifications. In comparison, normal scoped beans will exist 0-1 time in given context, so it's pretty straightforward there.

Therefore you will have to stick to non-conditional observer method for your @Dependent beans.