If I use both @PostConstruct and @Scheduled on a bean method what will be the consequences

564 views Asked by At
@Scheduled(fixedDelay=10000)
@PostConstruct
public void someMethod(){
 //my refresh cache code here
}

If I use both @PostConstruct and @Scheduled on a bean method what will be the consequences. Will this method will be executed twice? one after the other of may be at same time ?

1

There are 1 answers

0
Valerij Dobler On

The consequence will be that as soon as the class containing this method is created, the @PostConstruct will run. And the @Scheduled will trigger this method after 10_000ms, but only if @EnabledScheduling is added to the context.

The @Scheduled annotation can do both hence they can be merged like that

@Scheduled(fixedDelay=10000, initialDelay=0)
public void someMethod(){
 //my refresh cache code here
}