基于Spring注解方式配置Quartz

本文详细介绍如何使用Spring Quartz的基本注解方式配置定时任务。通过示例代码展示如何定义需要调度的类并添加注解,以及如何在Spring配置文件中启用注解驱动和组件扫描,实现更简洁的定时任务管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前我们都是通过基于XML的方式实现Spring  Quartz 虽然配置起来特别的方便,但是Spring还支持基本注解的方式来配置,这样做不仅更加简单,而且代码量也更加少了很多。

1、配置需要调度的类,并添加注解

import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
@Component
public class HelloJob {
 
    public HelloJob() {
        System.out.println("HelloJob创建成功");
    }
    @Scheduled(cron = "0/1 * *  * * ? ")
    // 每隔1秒隔行一次
    public void run() {
        System.out.println("Hello MyJob  " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));
    }
}

2、首先要配置我们的beans.xml,在xmlns 多加下面的内容
xmlns:task="http://www.springframework.org/schema/task" 

3、然后xsi:schemaLocation多加下面的内容
http://www.springframework.org/schema/task  
http://www.springframework.org/schema/task/spring-task-3.0.xsd

4、自动配置扫描spring配置文件里面配置内容
<!--开启这个配置,spring才能识别@Scheduled注解-->  
<task:annotation-driven/>  
<!-- 自动扫描注解的bean -->  
<context:component-scan base-package="com.binnor"/> 

 
原文:https://blog.youkuaiyun.com/tanyongbing1988/article/details/45689987 
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值