SSM整合SpringTask定时任务基于注解的使用
1.重点是引入task的schema:
1 2 | xmlns:task= "http://www.springframework.org/schema/task" http: //www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd |
2.注解使用必须在xml中开启注解任务,使注解生效:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?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: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.xsd http: //www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启动注解驱动的spring mvc 功能 --> <mvc:annotation-driven /> <!-- 启动包扫描功能 --> <context:component-scan base -package= "com.qingfeng.controller" /> <context:component-scan base -package= "com.qingfeng.service" /> <!-- 开启任务调度注解--> <task:annotation-driven ></task:annotation-driven>> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/views/" /> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
3.SpringTaskJob定时类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.qingfeng.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component //把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> public class SpringTaskJob { @Scheduled(cron = "0/2 * * * * ?" ) public void cron() { System. out .println( "执行定时任务,当前时间:" +System.currentTimeMillis()); } } |
4.输出结果
1 2 3 4 5 6 7 | 执行定时任务,当前时间:1602335496018 执行定时任务,当前时间:1602335498001 执行定时任务,当前时间:1602335500001 执行定时任务,当前时间:1602335502001 执行定时任务,当前时间:1602335504001 执行定时任务,当前时间:1602335506001 执行定时任务,当前时间:1602335508008 |
分类:
SSM
, SpringTask
标签:
SpringTask
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性