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

@Scheduled methods in @Lazy beans are not scheduled [SPR-17587] #22119

Closed
spring-projects-issues opened this issue Dec 10, 2018 · 1 comment
Closed
Assignees

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Dec 10, 2018

Silvio Assunção Junior opened SPR-17587 and commented

The issue #17470 was not resolved,  the problem continue

I checked the code of ScheduledAnnotationBeanPostProcessor and the tasks will registered on method postProcessAfterInitialization, but lazy Beans it is not created or initialized, than Beans with @Scheduled and @Lazy does not work correctly

The Schedule will be registered only if I will use the Bean (Force a creation)

Other guys with the same problem https://stackoverflow.com/questions/47585846/why-spring-schedule-do-not-work-with-lazy


Affects: 4.3.18

Reference URL: #17470

Issue Links:

  • #17470 @Scheduled methods in @Lazy beans are not scheduled
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 10, 2018

Juergen Hoeller commented

This works as designed: @Scheduled will only be processed on beans while they are 'alive', i.e. after creation and before destruction of each individual bean instance. For lazy-init singletons, this lifecycle only starts once they have actually been created.

If you want a singleton bean's scheduled methods to be called from startup onwards, simply don't declare it as lazy. If you want some backend state to only be created once actually called, factor it out to a separate bean that your @Scheduled bean refers to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.