Spring Boot FAQ: Top Questions
12. How do you enable scheduling in a Spring Boot application?
Spring Boot allows scheduling of tasks using the @Scheduled
annotation. You must enable it using @EnableScheduling
.
πΊοΈ Steps:
- Add
@EnableScheduling
in your main configuration class. - Annotate a method with
@Scheduled
.
π₯ Example:
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("Task runs every 5 seconds");
}
π Expected Output:
Console prints message every 5 seconds.
π οΈ Use Cases:
- Periodic data polling.
- Running maintenance tasks.