-
Notifications
You must be signed in to change notification settings - Fork 81
Description
The original issue and discussion can be found under weld/core#3191.
The disputed specification text is the following section for AfterBeanDiscovery (mind the emphasis made in the quote):
addBean()fires an event of typeProcessSyntheticBeancontaining the given Bean *and then* registers the Bean with the container, thereby making it available for injection into other beans. The given Bean may implement Interceptor or Decorator.
This wording makes a difference in case two or more extensions (or rather observers) are attempting to both, monitor the ProcessSyntheticBean event and conditionally register synthetic bean. I.e. should the ProcessSyntheticBean be fired in between notifications of other AfterBeanDiscovery observers or should all the ABD observers be fired first, and then fire ProcessSyntheticBean events?
Weld chooses to notify all of the ABD observers before firing ProcessSyntheticBean events. Below is the case I presented in the linked issue.
Imagine the following situation, should you be allowed to intertwine various lifecycle observer notifications:
- Two CDI extensions, let's call them
ExtensionAaExtensionB- They have no ordering of events nor do they know about each other (which is pretty common and perfectly fine in bigger apps)
ExtensionAalways unconditionally registers a bean with typeFoo(and no qualifiers) as a synthetic bean during ABD eventExtensionBmonitorsProcessSyntheticBean<Foo>, make note of its presence/absence and then in ABD attempt to add a synthetic beanFooif it was missing
Now, since CDI imposes exactly no rules on ordering of extensions, two situations can occur.
First one:
ExtensionAis notified first and registers synth beanFooExtensionBgets theProcessSyntheticBeanevent delivered, makes note of it and in its own execution skip the registration of the bean- Your app runs fine
Second one:
ExtensionBis notified first; as there is noFoobean yet, it registers itExtensionAthen registers the bean as well- You now have ambiguous dependency resolution
Cc @ljnelson and @Ladicek who participated in the discussion. Let's move it here and see what others think about it. We could/should also discuss it in the next CDI call.
As a side note - a very similar situation applies to registering a synthetic annotated type (which triggers PAT event).