Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

java - How does Spring's @Scheduled actually work?

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.

question from:https://stackoverflow.com/questions/65872052/how-does-springs-scheduled-actually-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...