![](/img/trans.png)
[英]Spring behaviour if a java method is annotated with both @Bean and @PostConstruct
[英]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.