spring quartz基于注解和配置文件的实现

一:基于配置文件的实现

1.导入相关jar包:

quartz-all-1.8.5.jar ; spring-context-support-3.0.1.jar;

如果报这样的错:java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 则继续添加 aopalliance-alpha1.jar

2.创建定时器任务调度类

package com.right.quartz;

import java.util.Date;

public class MyQuartz {
	
	public void sayHello(){
		System.out.println(new Date()+"Hello everybody! My name is spring quartz!");
	}

}

3.配置applicationContext.xml

 <!-- spring定时器 -->
	 <bean id="myQuartz" class="com.right.quartz.MyQuartz"></bean> <!-- 需要调用的类 -->
	 
	 <bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!-- 配置调度任务 --> 
        <property name="targetObject"><!-- 调用的bean(类) -->
            <ref bean="myQuartz"/>  
        </property>  
        <property name="targetMethod"><!-- 调用的方法 -->
            <value>sayHello</value>  
        </property>  
    </bean>
    
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><!-- 配置任务时间策略触发器 -->
	    <property name="jobDetail"><!-- 调用的任务 -->  
	        <ref bean="jobTask"/>  
	    </property>  
	    <property name="cronExpression"><!-- 任务执行时间 -->
	        <value>0/5 * * * * ?</value><!-- 每五秒执行一次 -->  
	    </property>  
    </bean>
    
    <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 管理所有时间策略的触发器并启动他们 -->  
        <property name="triggers">  
            <list>  
                <ref local="cronTrigger"/>
            </list>  
        </property>  
    </bean>

4.大致的图解


二:基于注解的实现

1.导入相关jar包:同上;

2.编写定时器任务调度类:

package com.right.quartz;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class YouQuartz {
	
	@Scheduled(cron="0/5 * * * * ?")
	public void sayHello(){
		System.out.println(new Date()+" This is my first annotation quartz! So fuck you!");
	}
	
}
首先将类注解成组件:@Component ,交托到容器管理; 然后在需要调用的方法上注解@Scheduled 参数是时间策略,这里是代表每5秒执行一次,至于时间策略举起规则,在文章最后将会说明.

3.配置applicationContext.xml配置文件

首先添加头部申明:

xmlns:task="http://www.springframework.org/schema/task" 
http://www.springframework.org/schema/task  <!--这里是在xsi:schemaLocation中添加的-->
http://www.springframework.org/schema/task/spring-task-3.0.xsd
设定扫描的位置(找有注解的类或者方法):

 <context:component-scan 	base-package="*"/><!-- 全盘扫描 -->
开启注解:

<context:annotation-config/> <!-- 开启注解 -->
开启定时器:

<task:annotation-driven/><!-- 开启定时器 -->


整个过程就是这样!

PS:相比于配置文件,采用注解的方式明显少了好多配置,这样可以拜托臃肿的配置文件的烦恼.特别是有很多定时器的时候,配置文件会一个一个的配置,注解就简单多了!


下面简单来说说时间策略的规则(若想更复杂的请自行搜索资料):

首先时间格式: [秒] [分] [小时] [日] [月] [周] [年]
秒 : 0-59  , - * /  
分 : 0-59  , - * /  
时 : 0-23 , - * /  

日 : 1-31 , - * ? / L W  
月 : 1-12 or JAN-DEC , - * /  
周 : 1-7 or SUN-SAT , - * ? / L #   
年 : empty 或 1970-2099 , - * /  

上文中提到的时间策略 : 0/5 * * * * * ? 表示0秒启动,每5秒执行一次,不在乎哪一年! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quartz整合Spring Boot中,可以通过读取配置文件配置Quartz的属性。具体步骤如下: 1. 在Spring Boot的配置文件(如application.properties或application.yml)中添加Quartz的相关配置信息。例如,可以配置Quartz的调度线程数、线程优先级、作业存储方式等。可以参考引用中提供的源码链接来查看具体的配置文件。 2. 在Spring Boot应用程序的启动类上使用`@EnableScheduling`注解开启定时任务。 3. 在需要执行定时任务的方法上使用`@Scheduled`注解,并指定定时任务的表达式或固定时间间隔。这些注解配置可以让Spring Boot自动创建和管理Quartz的调度器和作业。 需要注意的是,Quartz配置和作业的定义并不需要额外的代码来读取配置文件Spring Boot会自动将配置文件中的配置项注入到相关的Quartz对象中,从而实现整合。因此,只需要在配置文件中正确设置Quartz相关的属性即可。 总而言之,通过在Spring Boot的配置文件配置Quartz的属性,并在启动类和定时任务方法上使用相应的注解,就可以实现QuartzSpring Boot的整合,并读取配置文件配置Quartz。这样就可以方便地实现定时任务的管理和调度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot 整合 Quartz](https://blog.csdn.net/weixin_48220189/article/details/128645014)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot整合Quartz](https://blog.csdn.net/weixin_38192427/article/details/121111677)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值