java定时器注解形式的

步骤:

1、配置spring的配置文件 spring.xml,加入命名空间

[html]  view plain  copy
  1. xmlns:task="http://www.springframework.org/schema/task"    

2、在spring.xml配置文件中加入schema资源

[html]  view plain  copy
  1. http://www.springframework.org/schema/task    
  2. http://www.springframework.org/schema/task/spring-task-3.1.xsd  

3、spring.xml配置文件中加入是定时任务注解启用的标签

[html]  view plain  copy
  1. <task:annotation-driven/>   

完整配置如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    
  4.     xmlns:tx="http://www.springframework.org/schema/tx"     
  5.     xmlns:task="http://www.springframework.org/schema/task"  
  6.     http://www.springframework.org/schema/beans    
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.     http://www.springframework.org/schema/tx    
  9.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  10.     http://www.springframework.org/schema/aop    
  11.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  12.     http://www.springframework.org/schema/task    
  13.     http://www.springframework.org/schema/task/spring-task-3.0.xsd  ">    
  14.     <bean id="singer" class="com.fan.demo.TimesTasks"/>    
  15.    <task:annotation-driven />    
  16. </beans>   
Java代码如下:
[java]  view plain  copy
  1. @Component  
  2. public class EbFreightTask {  
  3.     private static final Logger logger = Logger.getLogger(EbFreightTask.class);  
  4.     @Autowired  
  5.     private EbFreightService ebFreightService;  
  6.     //@Scheduled(cron="0/30 * *  * * ? ")   //每30秒执行一次  
  7.     //@Scheduled(cron="0 0/6 * * * ? ")     //每1分钟调用一次  
  8.     //@Scheduled(cron = "0 0 0/1 * * ?")    //每隔一小时执行一次  
  9.     //@Scheduled(cron="0 0 1 * * ?")        //每天凌晨一点调用  
  10.     @Scheduled(cron = "0 0 0 * * ?")        //每天零点开始执行      ##更多cron表达式请自行搜索  
  11.     public void bTask() {  
  12.         logger.info(this.getClass()+"定时任务开始执行……");  
  13.         try {  
  14.             Date date = new Date(System.currentTimeMillis());  
  15.             DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  16.             ebFreightService.ExecuteOperation();   //定时任务需要具体实现的功能  
  17.         }catch (Exception e){  
  18.             logger.error(e);  
  19.         }  
  20.     }  
  21. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和提供了两个关于Spring定时器的示例代码。在Spring框架中,使用@Scheduled注解来标记一个方法作为定时任务。这个注解可以接收一个cron表达式参数,用于指定任务的执行时间。cron表达式是一种用于定义时间表的字符串,它可以精确到秒级别。例如,"@Scheduled(cron = "0/5 * * * * ?")"表示任务每隔五秒执行一次,而"@Scheduled(cron = "0/10 * * * * ?")"表示任务每隔十秒执行一次。 引用提供了Spring框架中用于定义定时任务的XML配置文件的命名空间和架构。使用这些命名空间和架构可以在XML文件中定义定时任务的配置信息。 所以,对于Spring的定时器注解,你可以使用@Scheduled注解来标记一个方法作为定时任务,并通过cron表达式来指定任务的执行时间。你也可以使用XML配置文件来定义定时任务的配置信息。123 #### 引用[.reference_title] - *1* [Spring定时器注解配置](https://blog.csdn.net/weixin_33966095/article/details/85922020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* [Spring定时器注解](https://blog.csdn.net/iteye_6147/article/details/82609534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [spring 定时器注解方式](https://blog.csdn.net/cibao3262/article/details/100619974)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值