且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring框架中的Quartz定时任务使用笔记(通过@Scheduled注解的方式实现)

更新时间:2022-09-10 08:54:53

1.修改spring的xml配置信息 applicationContext.xml 三个部分内容

1.xmlns添加:
xmlns:task="http://www.springframework.org/schema/task"
2.xsi:schemaLocation添加:
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
3.applicationContext.xml中添加:
<task:annotation-driven/>

2.最后编写需要定时执行的方法即可,加上对应的注解

package spring.demo.service;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
@Service
public class QuartzService {
    //这里有7个时间参数,有不懂的可以直接百度Quartz时间参数详解
    @Scheduled(cron = "0/2 * * * * *")
    public void process() {
        System.out.println("job run...");
    }
   
}