Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bean creation lazily ignores annotations like @EventListener #34057

Closed
nmirzadeh opened this issue Dec 9, 2024 · 1 comment
Closed

Bean creation lazily ignores annotations like @EventListener #34057

nmirzadeh opened this issue Dec 9, 2024 · 1 comment
Assignees
Labels
type: documentation A documentation task
Milestone

Comments

@nmirzadeh
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 9, 2024
@snicoll snicoll transferred this issue from spring-projects/spring-boot Dec 9, 2024
@snicoll
Copy link
Member

snicoll commented Dec 10, 2024

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.

@snicoll snicoll added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 10, 2024
@snicoll snicoll added this to the 6.2.x milestone Dec 10, 2024
@snicoll snicoll modified the milestones: 6.2.x, 6.2.2 Dec 12, 2024
@snicoll snicoll self-assigned this Dec 12, 2024
@sbrannen sbrannen changed the title Bean creation lazily ignores the annotations like @EventListener Bean creation lazily ignores annotations like @EventListener Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants