spring @Scheduled注解使用误区,强烈建议同胞们看。
来源:互联网 发布:哪个软件直播笑傲江湖 编辑:程序博客网 时间:2024/04/30 12:09
在使用spring @Scheduled注解时很多人都为cron表达式无法进行配置进行烦恼吧,为何不像quartz般能在applicationContext中进行配置。
告诉大家其实是能applicationContext进行配置。
xml:
<context:annotation-config />
<!--spring扫描注解的配置 --> <context:component-scan base-package="com.baodian.bdweb.timer" /> <!-- 开启这个配置,spring才能识别@Scheduled注解 --><task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>
java:
@Component("Taskaa")public class Taskaa{@Scheduled(cron = "01 50 15 * * ?")public void show(){SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("Taskaa:"+s.format(new Date())); }}<pre name="code" class="java">@Component("Taskbb")public class Taskbb{@Scheduled(cron = "01 49 15 * * ?")public void show(){SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("Taskbb:"+s.format(new Date())); }}
上面这一种方式是大家最头痛的问题吧,如果测试人员需要测试这个任务,你需要不停生成class文件给测试人员替换,是不是非常郁闷,其实我前期也被逼疯了。我前期一直使用的Quartz,因此对该注解不是非常熟悉,趁着这个闲时想看看这个注解,结果一百度,却找不到想要的结果。。换上我的谷歌继续,功夫不负有心人,找到了一篇该注解的详解,因此有了这一篇文章。废话不多说,看原子弹来了!
xml:
<context:annotation-config /> <!--spring扫描注解的配置 --> <context:component-scan base-package="com.baodian.bdweb.timer" /> <bean id="Taskaa" class="com.baodian.bdweb.timer.Taskaa" /> <bean id="Taskbb" class="com.baodian.bdweb.timer.Taskbb" /> <task:scheduled-tasks> <task:scheduled ref="Taskaa" method="show" cron="01 38 15 * * ?" /> <task:scheduled ref="Taskbb" method="show" cron="01 39 15 * * ?" /> </task:scheduled-tasks>
java:
@Component("Taskaa")public class Taskaa{@Scheduledpublic void show(){SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("Taskaa:"+s.format(new Date())); }}
@Component("Taskbb")public class Taskbb{@Scheduledpublic void show(){SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("Taskbb:"+s.format(new Date())); }}
是不是很简单,嘿嘿嘿!原子弹就造到这里了,配置很简单在这里就不细说了,主要就是想吐槽一下百度,嘿嘿嘿!
0 1
- spring @Scheduled注解使用误区,强烈建议同胞们看。
- spring @Scheduled注解使用误区,强烈建议同胞们看。
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled 注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- easyUI批量删除,向后台传数组
- 15. 3Sum
- 欢迎使用CSDN-markdown编辑器
- typeof和instanceof用法
- C++ String类实现以及初始化原理和析构顺序
- spring @Scheduled注解使用误区,强烈建议同胞们看。
- ARM LINUX 扩展串口
- 13个应用案例,讲述最真实的大数据故事!
- 网上面试题(二)
- 开发问题-Syntax error, insert ";" to complete FieldDeclaration
- C#中提供服务方式ServiceLocator
- *.pyc文件无法执行
- 用C语言玩JSON
- 74. Search a 2D Matrix