最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java使用@Scheduled注解执行定时任务代码示例
时间:2021-01-13 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下java使用@Scheduled注解执行定时任务代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
前言
在写项目的时候经常需要特定的时间做一些特定的操作,尤其是游戏服务器,维护线程之类的,这时候就需要用到定时器。
如果此时你刚好用的是spring的话,哪么@Scheduled注解是非常好用的。
使用spring @Scheduled注解执行定时任务:
1,在spring-MVC.xml文件中进行配置
2,直接在代码控制层使用即可
package xkhd.game.fix; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 游戏数据表维护 * * @author Administrator * */ @Component @Lazy(value = false) public class fix_game { @Autowired private fix_Service fix_Service; /** * 每分钟 */ @Scheduled(cron = "0 */1 * * * ?") public void Everyminute_control() { System.out.println("***********每分钟"); fix_Service.Everyminute(); } /** * 每小时 */ @Scheduled(cron = "0 0 0/1 * * ?") public void Everyhours_control() { System.out.println("***********每小时"); fix_Service.Everyhours(); fix_Service.deleteUserlogincodeCt(); fix_Service.weixin(); } /** * 每天零点 */ @Scheduled(cron = "0 0 0 * * ?") public void Everyday_control() { System.out.println("***********每天零点"); fix_Service.Morningeveryday(); } }
-
下一个: vue动态设置路由权限的主要思路代码
相关文章
- SpringBoot自定义bean绑定解析 10-24
- Javaweb工程运行报错HTTP Status 404解决教程 10-20
- JAVA获取jvm和操作系统相关信息方法 10-20
- BeanFactory和FactoryBean的区别讲解 10-20
- 微信小程序的宿主环境实现教程 10-10
- dispatchEvent解决重叠元素响应事件教程 10-10