簡體   English   中英

使用@PostConstruct注釋的方法是否可以保證在同一個bean中使用@Scheduled的方法之前執行?

[英]Will a method annotated with @PostConstruct be guaranteed to execute prior to a method with @Scheduled within the same bean?

假設我正在定義一個緩存bean,它將在啟動應用程序時初始化,並將按固定間隔刷新,如下所示:

@Component
public class Cache {

     ...

     @PostConstruct
     public void initializeCache() {
     ...
     }

     @Scheduled(fixedRate = 60L * 1000L)
     public void refreshCache() {
     ...
     }

}

@PostConstruct下的initializeCache()方法是否可以保證在第一次調度的refreshCache()調用之前立即執行,即使我沒有設置初始延遲?

是的,保證使用@PostConstruct注釋的方法首先執行。

@Scheduled注釋由ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization()激活,該調用任何bean初始化回調之后調用(如InitializingBean的afterPropertiesSet或自定義init方法)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM