I'm trying get a class to request data from a rest api periodically to check if the data I have in my database is still up to date.
The documentation says that to enable scheduling I should add the @EnableScheduling
annotation to a configuration class, and that I will then be able to use the @Scheduled
annotation on any spring managed bean in the container.
It all works, but I don't fully understand what any spring managed bean in the container means. Right now I declared the bean like this in the configuration class (the class CapsuleRestApi
is the class responsible for requesting data from the api)
@Bean
public CapsuleRestApi capsuleDatabaseJpa() {
return new CapsuleRestApi();
}
And then I used this method in the CapsuleRestApi class
@Scheduled(fixedDelay = 2000)
public void refresh() {
// refresh and check changes
}
Is there another way to make it work without adding the method as bean? I don't fully understand why it works with the bean method.