Spring Boot Version: 2.4.2 Spring Framework Version: 5.3.3

  1. @EnableScheduling
  2. Create a @Component with a @Scheduled method
  3. Add spring.main.lazy-initialization=true to application.properties
  4. Start application

The method is never called.

Comment From: sbrannen

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Comment From: snicoll

Moving it back to Spring Boot as @wilkinsona reminded me we have dedicated logic in other areas to prevent that from happening.

Comment From: wilkinsona

A general-purpose fix for this would require us to mimic some of the logic in ScheduledAnnotationBeanPostProcessor. When scheduling and lazy initialization are enabled, we'd need a LazyInitializationExcludeFilter that excludes any bean with a @Scheduled- or @Schedules-annotated method. That doesn't feel too bad I suppose. The alternative would be to document this as a restriction. Let's see what the rest of the team thinks.

@arikanorh While we're deciding what to do, you should be able to work around the problem with one or more LazyInitializationExcludeFilters of your own. If you know the types that are using @Scheduled then you can do something like this:

@Bean
LazyInitializationExcludeFilter lazyInitializationExcludeFilter() {
    return LazyInitializationExcludeFilter.forBeanTypes(TypeWithScheduledMethods.class);
}

Comment From: snicoll

There are two issues here. The filtering we've described above and the fact our TaskScheduler isn't picked up by Spring Framework when it is flagged lazy (as this is the case for all beans in the context when the option is enabled). We're investigating if an issue in Spring Framework is necessary.