You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a scenario to create a bean lazily. The class annotation with @eventlistener. The object was created, but it didn't listen to any event. I had to change the class to implement EventListener.
I have created one small project to demonstrate the issue. There are two tests where one eagerly creates a bean, and the other one creates lazily. The failing one shows the bean doesn't have the point cut of @eventlistener annotation. I used other types of annotation (e.g. @Cacheable) bean-with-annotation.zip
, but the effect was the same.
The text was updated successfully, but these errors were encountered:
The arrangement is a bit weird, isn't it? If we fixed this issue to register the listener lazily, then your component would start listening to the chosen events, but only once "something" triggers the creation of the target bean.
We do loop over all beans after singletons are instantiated to register event listeners, if any. But we avoid doing so on lazy bean as it would defeat the purpose of making them as such. Your analogy with @Cacheable is incorrect as the operation is invoked on the method itself and doesn't depend on an external component doing something (publishing an event).
I can see how this could be more prominent in the documentation, though.
sbrannen
changed the title
Bean creation lazily ignores the annotations like @EventListener
Bean creation lazily ignores annotations like @EventListener
Dec 14, 2024
I have a scenario to create a bean lazily. The class annotation with @eventlistener. The object was created, but it didn't listen to any event. I had to change the class to implement EventListener.
I have created one small project to demonstrate the issue. There are two tests where one eagerly creates a bean, and the other one creates lazily. The failing one shows the bean doesn't have the point cut of @eventlistener annotation. I used other types of annotation (e.g. @Cacheable)
bean-with-annotation.zip
, but the effect was the same.
The text was updated successfully, but these errors were encountered: