简体   繁体   English

Spring 的@Scheduled 是如何工作的?

[英]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.我一直认为@Scheduled通过代理整个 bean 来工作,就像@Async@Transactional等一样。 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.谢谢。

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.简而言之,使用最简单的配置,当 Spring 检测到@EnableScheduling注解时,它会创建一个新的ScheduledAnnotationBeanPostProcessor来处理@Scheduled注解。 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 .在使用反射找到@Scheduled注释后,它将读取它们的配置,然后注册它们以便被TaskScheduler调用。 A ScheduledTaskRegistrar is used to help submit task to the ScheduledThreadPoolExecutor . ScheduledTaskRegistrar用于帮助将任务提交到ScheduledThreadPoolExecutor

As you can see, no proxy is being created.如您所见,没有创建代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM