需要購買阿里雲產品和服務的,點擊此鏈接領取優惠券紅包,優惠購買哦,領取后一個月內有效: https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=fp9ccf07
1、web.xml
2、spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 掃描controller(controller層注入) --> <context:component-scan base-package="com.lin.controller" /> <!-- 對模型視圖添加前后綴 --> <!-- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp"/> --> <!-- 上傳文件 --> <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 最大上傳尺寸 5M <property name="maxUploadSize" value="5242880"/> </bean> --> <!-- 定義受環境影響易變的變量 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="fileEncoding" value="utf-8" /> <property name="locations"> <list> <!-- 標准配置 --> <value> classpath*:/properties/config.properties </value> </list> </property> </bean> <!-- 增加定時任務插件 --> <context:annotation-config></context:annotation-config> <!-- spring 掃描注解配置 --> <context:component-scan base-package="com.lin.quartz"></context:component-scan> <!-- 開啟這個配置 spring才能識別@Scheduled注解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/> </beans>
3、QuartzTask.java
package com.lin.quartz; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.lin.service.SysUserService; @Component("taskJob") public class QuartzTask { @Autowired private SysUserService uService; /** * CRON表達式 含義 "0 0 12 * * ?" 每天中午十二點觸發 "0 15 10 ? * *" 每天早上10:15觸發 "0 15 10 * * ?" 每天早上10:15觸發 "0 15 10 * * ? *" 每天早上10:15觸發 "0 15 10 * * ? 2005" 2005年的每天早上10:15觸發 "0 * 14 * * ?" 每天從下午2點開始到2點59分每分鍾一次觸發 "0 0/5 14 * * ?" 每天從下午2點開始到2:55分結束每5分鍾一次觸發 "0 0/5 14,18 * * ?" 每天的下午2點至2:55和6點至6點55分兩個時間段內每5分鍾一次觸發 "0 0-5 14 * * ?" 每天14:00至14:05每分鍾一次觸發 "0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44觸發 "0 15 10 ? * MON-FRI" 每個周一、周二、周三、周四、周五的10:15觸發 */ /** * 每天5點觸發(清空驗證碼表t_captcha中的數據) */ @Scheduled(cron = "0 0 5 * * ?") public void testTask(){ System.out.println(System.currentTimeMillis()); Integer count = uService.clearCaptcha(); } }
需要購買阿里雲產品的,可以點擊此鏈接領取紅包,優惠購買哦: https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=fp9ccf07