关于spring mvc注解定时任务配置
简单的记载:避免自己忘记,不是很确定我理解的是否正确。有错误地方望请大家指出。
1,定时方法执行配置:
(1)在applicationContext.xml中加入以下配置
xmlns:task="http://www.springframework.org/schema/task"
在xsi:schemaLocation地址下引入
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
加入task引入
<task:annotation-driven/>
applicationContext.xml配置完成。
(2)在你需要定时的方法上面加入注解@Scheduled(fixedRate = 10 * 1000)
/** * 每隔十分钟执行一次 * @Scheduled(fixedRate = 60 * 10 * 1000) */ @Scheduled(fixedRate = 60 * 10 * 1000) public void handle() { //写入自己的逻辑代码 }
到此我理解的第一种结束
2,在配置用xml配置定时任务
(1)新建一个xml文件:配置定时任务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 自己定义的定时类 --> <bean id="smsAutoSendInfoQuartzJob" class="com.minxinloan.sms.auto.quartz.SmsAutoSendInfoQuartzJob" /><!-- 自己项目中需要定时的处理逻辑类 --> <bean id="smsAutoSendInfoDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="smsAutoSendInfoQuartzJob" /><!-- 注入需要定时执行的类 --> </property> <property name="targetMethod"> <value>atuoRemitInfoTask</value> <!-- 类中的方法名称配置 --> </property> </bean> <!-- 设置定时任务的时间 --> <bean id="smsAutoSendInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="smsAutoSendInfoDetail" /> </property> <property name="cronExpression"> <value>0 0 * * * ?</value> <!-- 这个可能是每天早上凌晨触发,这个参数我记不太清楚,自己可以根据需要查询 --> </property> </bean> <!-- end --> <!-- 启动触发器的配置开始 --> <bean name="startCrmQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="smsAutoSendInfoTrigger" /> </list> </property> </bean> <!-- 启动触发器的配置结束 --> </beans>
(2)需要的jar包
Spring-context-support-3.2.2.jar包
差不多我理解的到此结束。