Event-observer bean creation

When Weld is in the process of delivering events to observers during our call to Event.fire(), it will automatically instantiate the bean that defines the observer method to be called if there isn't currently an instance of that bean present within the current context.

For most cases that is perfectly fine, but there may be situations where that default behavior is unwanted. Thankfully, CDI allows us to define a conditional observer, such that the bean will only observe events if it's already present within the current context.

We can specify a conditional observer by setting a value for the notifyObserver member of @Observes.

public void refreshOnBookRemoval (@Observes( notifyObserver = IF_EXISTS ) @Removed Book bk) ...

Get JBoss Weld CDI for Java Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.