spring @Scheduled注解 定时任务 详解
2024-09-06 17:57:46
scheduled的使用注解的方式进行调度
先要配置spring.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.1.xsd
再加上扫描注解
<task:annotation-driven/>
然后扫描位置
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.zyt.test"/>
调用
import org.springframework.stereotype.Component; @Component
public class TestService {
// 5秒钟执行一次
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void test(){
System.out.println("每隔五秒打印一次");
}
}
@Scheduled 注解要写在实现方法上
定时器的任务方法不能有返回值(如果有返回值的话,spring初始化的时候会报错,需要设定一个proxytargetclass的某个值为true,请自行百度google)
剩下的就是corn表达式了
请自行百度google
最新文章
- Linux - Tips
- UITextFiled,UITextView长度限制
- vim——打开多个文件、同时显示多个文件、在文件之间切换
- [转]Ubuntu下使用Jni开发例子
- iOS prototype Cells
- HDU 1978 How many ways(动态规划)
- OPatch failed with error code 73
- codeforces Minesweeper 1D
- SSAS父子层次结构的增强-UnaryOperatorColumn属性
- Android动态加载技术(插件化技术)
- spring里的事物设置
- [题目] luogu P2061 [USACO07OPEN]城市的地平线City Horizon
- MySQL 术语
- HTML5 图片宽高自适应,居中裁剪不失真
- mybatis分页插件Mybatis_PageHelper 简单案例
- Okhttp3 使用和原理(DEMO)
- WPF导航总结
- C#扩展一个现有的类
- unicode编码转汉字
- bzoj3172: [Tjoi2013]单词 ac自动机