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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值