How does Spring's @Scheduled actually work?

Viewed 87

I always thought that @Scheduled works by proxying the whole bean, same way the @Async, @Transactional, etc does. So I was surprised that the following works like a charm:

    @Component
    public static class Bean {

        @Scheduled(fixedRate = 1000)
        private void scheduled() {
            System.out.println("Yo");
        }
    }

Did they change something or it's how it was from the very beginning? Thanks.

1 Answers

In a few words, with the simplest possible configuration, when Spring detects @EnableScheduling annotation it creates a new ScheduledAnnotationBeanPostProcessor which is able to process the @Scheduled annotations. After finding the @Scheduled annotations using reflection, it will read their configuration and then it will register them in order to be invoked by TaskScheduler. A ScheduledTaskRegistrar is used to help submit task to the ScheduledThreadPoolExecutor.

As you can see, no proxy is being created.

Related