Spring Task Scheduling: XML and Annotation Configurations with Cron Expressions
This article explains how to implement scheduled tasks in Spring using the lightweight Spring Task framework, covering both XML‑based and annotation‑based configurations, providing complete code examples, cron expression details, and practical tips for Java backend developers.
Spring provides built‑in support for scheduled tasks, and this article shows how to use Spring Task (a lightweight alternative to Quartz) with both XML‑based and annotation‑based configurations.
1. XML configuration method
First, create a simple POJO class that contains the task method:
package com.aflyun.web.task;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
//@Service also works
public class TaskCool {
/**
* First timer test method
*/
public void testJob() {
System.out.println("test first taskJob .... ");
}
}Then configure applicationContext.xml with the required namespaces and a task:scheduled-tasks element:
Note that the task namespace and schema must be added to the configuration file, and the ref attribute defaults to the bean name (the class name with a lower‑case first letter).
2. Annotation‑based method
Using @Scheduled eliminates the need for XML entries for each task. The annotation definition in Spring looks like this:
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scheduled {
public abstract String cron();
public abstract long fixedDelay();
public abstract long fixedRate();
}Key attributes:
cron – a cron expression that defines the schedule.
fixedDelay – interval in milliseconds between the end of the previous execution and the start of the next.
fixedRate – interval in milliseconds between the start of successive executions.
Example POJO using the annotation:
package com.tclshop.cms.center.web.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class WebTask {
// Execute every 5 seconds
@Scheduled(cron = "0/5 * * * * ?")
public void TaskJob() {
System.out.println("test second annotation style ...");
}
}To enable annotation processing, add the following to applicationContext.xml :
Only the task:annotation-driven line is strictly required; other parameters are optional.
3. Cron expression reference
The cron fields and allowed values are:
Field Allowed values Special characters
Second 0-59 , - * /
Minute 0-59 , - * /
Hour 0-23 , - * /
Day 1-31 , - * ? / L W C
Month 1-12 or JAN-DEC , - * /
Weekday 1-7 or SUN-SAT , - * ? / L C #
Year (optional) 1970-2099 , - * /Common examples:
"0 0 12 * * ?" – every day at 12:00 PM
"0 15 10 ? * *" – every day at 10:15 AM
"0 0/5 14 * * ?" – every 5 minutes between 2 PM and 2:55 PM
"0 10,44 14 ? 3 WED" – at 14:10 and 14:44 on every Wednesday in March
"0 15 10 ? * MON-FRI" – at 10:15 AM Monday through Friday4. Summary
Spring’s task scheduling can be set up quickly with either XML configuration or the more concise @Scheduled annotation, both supporting flexible cron expressions without requiring external libraries.
References
Spring scheduled task implementations – http://gong1208.iteye.com/blog/1773177
Cron expression guide for Spring – http://www.cnblogs.com/xiaopeng84/archive/2009/11/26/1611427.html
Quartz cron expression details – http://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html
Original article: blog.csdn.net/u010648555/article/details/52162840
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.