spring kotlin cron
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
@Component
class MyScheduledTask {
@Scheduled(cron = "0 0 12 ?")
fun executeTask() {
// Your task logic goes here
println("Task executed at 12:00 PM every day")
}
}
Explanation:
import org.springframework.scheduling.annotation.Scheduled
: Imports theScheduled
annotation from the Spring Framework, used for scheduling tasks.import org.springframework.stereotype.Component
: Imports theComponent
annotation from the Spring Framework, indicating that the class is a Spring component and can be managed by the Spring container.@Component
: Annotates theMyScheduledTask
class as a Spring component, allowing it to be automatically detected and managed by the Spring container.@Scheduled(cron = "0 0 12 ?")
: Annotates theexecuteTask
method with theScheduled
annotation and specifies a cron expression"0 0 12 ?"
to schedule the method execution. This cron expression signifies the task will be executed at 12:00 PM every day.fun executeTask() { ... }
: Defines theexecuteTask
method where you can implement the logic of the task that will be executed according to the specified cron expression.println("Task executed at 12:00 PM every day")
: Represents a placeholder for the actual task logic to be executed at the specified schedule. In this case, it prints a message indicating the task execution at 12:00 PM every day. You can replace this line with the actual functionality needed for your task.